$ sudo 'cd /root/restricted'
Password:
sudo: cd /root/restricted: command not found
$
cd Not Found ?? That is RIGHT !
cd is a shell built-in command. It cannot be run in a child process. The child process simply cannot change the working directory of its parent shell process.
Redirection also does not work with sudo for the same reason (redirection being a shell "thing")
$ sudo 'ls /root/restricted >/root/out.txt'
sudo: ls /root/restricted >/root/out.txt: command not found
$
The workaround in both cases is to execute the command in a subshell.
$ sudo sh -c 'cd /root/restricted'
$ sudo sh -c 'ls /root/restricted >/root/out.txt'

0 comments:
Post a Comment