12

I wrote a bash script to add to my $PYTHONPATH. My .sh file has the following:

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

What I want to be added to my .bashrc is:

PYTHONPATH=$PYTHONPATH:/path/to/new/python/module

However I can only get it to add:

PYTHONPATH=/all/other/python/modules/already/on/path:/path/to/new/python/module

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

2 Answers 2

13

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]

Sign up to request clarification or add additional context in comments.

2 Comments

Maybe emphasize that it's lucky that the sudo doesn't do anything here; if it did, you could create a file which the user cannot later modify. Luckily, here, the sudo is a complete no-op, because the redirection causes the user's shell to create the file before sudo echo starts executing.
@tripleee Very good point. Answer updated with sudo-echo discussion.
-1

try

echo 'export PYTHONPATH=/path/to/caff-dir/python'

Also, you may need to run following:

pip install -r requirement.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.