Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In Linux I can well to add alias to bashrc and it will become a permanent alias. In Mac OS I tried to do the same thing:

vim ~/.bashrc

export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH f  or scripting
alias prj="cd ~/Documents/projects"

### Added by the Heroku Toolbelt
export PATH="/usr/local/heroku/bin:$PATH"

That being said, I got this:

$ alias
alias rvm-restart='rvm_reload_flag=1 source '\''/Users/alex/.rvm/scripts/rvm'\'''

So where is my prj alias? I rebooted the laptop but nothing has changed.

$ cat ~/.profile

export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
test -f ~/.bashrc && source ~/.bashrc

$ cat ~/.bashrc
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH f  or scripting
alias prj="cd ~/Documents/projects"

### Added by the Heroku Toolbelt
export PATH="/usr/local/heroku/bin:$PATH"
share|improve this question

2 Answers 2

up vote 1 down vote accepted

Usually, bash only sources your ~/.bashrc startup script file for interactive, non-login shells.
Usually, bash only sources your ~/.profile startup script file for interactive login shells.
Usually, Terminal.app treats new terminal windows as interactive login shells.
So in normal circumstances, only your ~/.profile gets read and executed; your ~/.bashrc never gets read and executed.

To solve this, I usually create the shell startup script named ~/.profile (if it doesn't already exist) and I add the following command to that file so that it checks to see if the ~/.bashrc file exists, and it sources it (reads it in and executes it within the current shell process, not subshell) if it exists:

test -f ~/.bashrc && source ~/.bashrc

Rephrasing as requested: Mac OS X's Terminal app usually runs your shell (bash) in "login" mode. When bash is run in login mode, it doesn't read/execute ~/.bashrc. Instead, it reads and executes ~/.profile. If you want to put things in your ~/.bashrc and have them read and executed in every new Terminal window, you need to create a shell script file called ~/.profile, and have that file contain a command that looks for your ~/.bashrc and reads and executes it.

In case the verb "source" threw you off: "sourcing" a shell script file is when a shell (such as bash) reads shell commands from a file and runs those commands in the current shell process, so they affect the environment of the current shell process. Usually other shell script files you write are not sourced; instead, they are executed in a sub-shell (a separate instance or copy of bash), and they can't change anything in the environment of their parent shell (the main instance of bash that was started when you opened the new Terminal window).

To get a shell to source a script, you use the source command, which is built into the shell. A shortcut for the source command is . (yep, just a dot/period/full-stop character). In a previous version of this Answer, I used the . operator instead of the source command.

share|improve this answer
    
I didn't get it, can you re-phrase that? –  Alex Jun 11 at 3:37
    
but it works, nonetheless. but only when I run this command. when I open another shell "prj" doesn't work anymore. –  Alex Jun 11 at 3:37
    
@Alex Okay, try now. –  Spiff Jun 11 at 5:16
    
thanks, but what is ... runs your shell (bash) in "login" mode and what are the other modes exist and difference between them? –  Alex Jun 11 at 5:53
    
@Alex That's a topic that deserves its own Question. In fact it's already been asked and answered right here on SuperUser: superuser.com/questions/657848/… –  Spiff Jun 11 at 6:15

Did you open a new terminal window or run source ~/.bashrc after editing? The current terminal needs to be updated for the paths/aliases.

$ vim ~/.bashrc
alias prj='cd ~'
:x
$ alias
$ alias rvm-restart='rvm_reload_flag=1 source '\''/Users/dx072/.rvm/scripts/rvm'\'''
$ source ~/.bashrc
$ alias
$ alias prj='cd ~'
  alias rvm-restart='rvm_reload_flag=1 source '\''/Users/dx072/.rvm/scripts/rvm'\'''
share|improve this answer
    
I said, "even after rebooting". –  Alex Jun 11 at 3:33
    
Right, so if the answer you accepted works, then this does as well. It's the same. –  h4xnoodle Jun 11 at 16:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.