You can do this:
echo 'export PATH=$PATH:/path/to/whatever' >> ~/.bashrc
This will append the line export PATH=$PATH:/path/to/whatever
to the end of your ~/.bashrc
file if it exists, or create the file and write the line to it if the file does not exist.
If you're not sure about the file existing, then this is a safe way to write (or create) the file.
You can also use if [ -f $PATH_TO_FILE ] ; then $DO_SOMETHING ; fi
expression to conditionally operate on a file if it exists.
At any rate, yes it is safe to create the file - and simply using echo
with the append-to-file redirect operator is a safe and easy way to do what you want. No need for a text editor or anything.
You can check that it works:
cat ~/.bashrc
This should print the bashrc file.
Now you can apply this to your current session (without having to open a new terminal window) and verify that your PATH
is being set correctly like so:
source ~/.bashrc && echo $PATH
getent passwd $(whoami) | awk -F: '{print $NF}'
say? – Joseph R. Oct 23 '13 at 20:35echo "PATH=$PATH:/path/to/whtaever" >> ~/.bashrc && source ~/.bashrc
– Charles Boyd Oct 24 '13 at 06:53$PATH
before the output gets redirected to the file. I wrote an answer below that gives correct command. – Charles Boyd Oct 24 '13 at 07:01