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?

Joseph R.
  • 39,549
bernie2436
  • 6,655

5 Answers5

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.

Joseph R.
  • 39,549
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

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.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
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.

GAD3R
  • 66,769
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).

peterph
  • 30,838