On my CentOS 7 machine, there are 5 folders in /home/nanxiao/log/:
$ ll /home/nanxiao/log/
total 0
drwxr-xr-x. 2 nanxiao nanxiao 100 Feb 28 15:56 facebook
drwxr-xr-x. 2 nanxiao nanxiao 102 Feb 28 15:56 instagram
drwxr-xr-x. 2 nanxiao nanxiao 98 Feb 28 16:00 netflix
drwxr-xr-x. 2 nanxiao nanxiao 96 Feb 28 15:56 tiktok
drwxr-xr-x. 2 nanxiao nanxiao 98 Feb 28 15:56 youtube
$
The following command echo the folder name:
$ for f in /home/nanxiao/log/*; do echo $f; done
/home/nanxiao/log/facebook
/home/nanxiao/log/instagram
/home/nanxiao/log/netflix
/home/nanxiao/log/tiktok
/home/nanxiao/log/youtube
$
But when I run the above command in sudo -i -u nanxiao, it outputs nothing:
$ sudo -i -u nanxiao bash -c 'for f in /home/nanxiao/log/*; do echo $f; done'
$
But if execute sudo -i -u nanxiao first, then the outputs are correct:
$ sudo -i -u nanxiao
$ bash -c 'for f in /home/nanxiao/log/*; do echo $f; done'
/home/nanxiao/log/facebook
/home/nanxiao/log/instagram
/home/nanxiao/log/netflix
/home/nanxiao/log/tiktok
/home/nanxiao/log/youtube
$
Why doesn't "sudo -i -u user bash -c string" run as expected?
sudo -iin this case? Possibly related:sudo -s <command>runs command in a shell, but wildcards or metacharacters not working.bash -c sudo -i -u user string?