cmoses cmoses - 11 months ago 64
Linux Question

Adding to $PYTHONPATH with bash script

I wrote a bash script to add to my

. My
file has the following:

sudo echo export PYTHONPATH=$PYTHONPATH:/path/to/new/python/module >> ~/.bashrc

What I want to be added to my


However I can only get it to add:


I don't want the actual
value to be added to my
, just the variable name. Please help!


Use single-quotes:

$ echo 'export PYTHONPATH=$PYTHONPATH:/path/to/new/python/module' >> .bashrc
$ cat .bashrc 
export PYTHONPATH=$PYTHONPATH:/path/to/new/python/module

The shell does not perform variable expansion on single-quoted strings.

Note also that, if you are writing to ~/.bashrc, you should not need sudo. A user should own his own ~/.bashrc. Further, as written, the sudo command only operated on echo. The redirection >~/.bashrc is done with the user's level of permission. Since echo has no need of and gets no benefit from sudo, sudo is a practically a no-op. [Hat tip: tripleee]