• Uncategorized

About linux : why-the-current-working-path-is-the-initial-path-in-shell

Question Detail

When I execute the following command:

[root@localhost install_zone]# su nylws -c “cd ~/gamesvr/conf;echo $(pwd);pwd”

The output:

/opt/deploy_scripts/install_zone

/data/nylws/gamesvr/conf

So why the two paths are different? Thanks!

Question Answer

It’s because $(...) is evaluated inside the double quotes by the parent shell, before the shell invoked by su is even started. So the subshell sees:

cd ~/gamesvr/conf;echo /opt/deploy_scripts/install_zone;pwd

The best way to fix this is to use single quotes, which will pass the string literally to the subshell and not do any expansion on it:

su nylws -c 'cd ~/gamesvr/conf;echo $(pwd);pwd'

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.