QUESTION :
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"
ANSWER :
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.
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''''