2

I am trying to add a location to my $PATH variable on a Mac running OS X. I am following this tutorial, which says to edit the .bashrc file. I don't see this file in the home directory even when I do

cd ~/ 
ls -A 

Can I just go ahead and create that file? Or am I missing something?

4
  • Are you sure you're running Bash? What does getent passwd $(whoami) | awk -F: '{print $NF}' say?
    – Joseph R.
    Oct 23, 2013 at 20:35
  • 8
    "Can I just go ahead and create that file?" Yes.
    – goldilocks
    Oct 23, 2013 at 20:44
  • echo "PATH=$PATH:/path/to/whtaever" >> ~/.bashrc && source ~/.bashrc Oct 24, 2013 at 6:53
  • I made a small mistake above - that needs to be in single quotes rather than double quotes so the shell doesn't expand $PATH before the output gets redirected to the file. I wrote an answer below that gives correct command. Oct 24, 2013 at 7:01

5 Answers 5

7

If the file really does not exist and you are actually running Bash, you can probably find a working copy to get you started (it has lots of examples for common usage in the comments) at /etc/skel/.bashrc. This is the .bashrc that gets added to the home directory of every new user by default. At least that's how it goes for most GNU/Linux distributions.

It is important to make sure that you're running Bash first, though, because if you're running some other shell, it will not read any .bashrc file you create or copy.

Find out the default shell for your user account from:

getent passwd $(whoami) | awk -F: '{print $NF}'

Find out the currently running shell from:

ps -p $$ -o cmd=

Updates

  • Under Mac OS X, you won't find /etc/skel, you should rather look under /System/Library/User\ Template/English.lproj/ as explained in this SU Q&A

  • A search prompted by your other question reveals that getent is indeed not available under Mac OS X. My bad for suggesting it. I really think you should have put this in a comment on the answer rather than starting a new question, though.

3
  • echo $SHELL or env | grep SHELL would suffice... Oct 25, 2013 at 4:03
  • @CharlesBoyd Not really. My default login shell is /bin/bash. When I enter csh and do echo $SHELL, I still get /bin/bash.
    – Joseph R.
    Oct 25, 2013 at 11:27
  • Yes, but your default shell is /bin/bash so your ~/.bashrc file will get read by your default shell upon login. Oct 25, 2013 at 16:31
2

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

0

Any reasonable editor will create the file that you try to write if it doesn't exist. Hence running

your_favourite_editor ~/.bashrc

will either open the existing file or an empty one. Once you save it, the file will be either overwritten (it it existed) or created (if it didn't). This is not even UNIX specific (i,e. it works on Windows as well).

It is safe to create the file if it doesn't exist - see section INVOCATION of the bash(1) man page. You might want to source ~/.bash_profile as the first thing there (if you have that file of course).

2
  • OP is asking if it's safe to create the new file, not how.
    – strugee
    Oct 24, 2013 at 2:51
  • @strugee gah, I've sent the answer too quickly, thanks for pointing that out.
    – peterph
    Oct 24, 2013 at 10:08
0

Also, If you decide to use both ~/.bashrc & ~/.bash_profile make sure you do not set up a recursive loop by sourcing one after the other. i.e. calling the ~/.bash_profile from the ~/.bashrc script will continually loop and create a new shell each time it starts up, filling your "/tmp & swap" space. If you are the "root" user this may crash the box if you let it run long enough.

0

You can following these steps:

  1. echo "PATH=$PATH:/path/to/whatever" >> ~/.bashrc && source ~/.bashrc

now you can open the bashrc with gedit

  1. gedit .bashrc

You can also edit username root.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .