I needed bash to source my .bashrc upon login to a new terminal on my mac (because it doesn't do that upon login to a new terminal on macs, it only does .bash_profile). I found and successfully used this command and added it to my .bash_profile:

if [ -f ~/.bashrc ] && [ "${SHELL##*/}" == "bash" ]
  . ~/.bashrc

There are three things about this command I don't understand.

  1. What does the -f mean in the first condition?
  2. What does the ##/ indicate in the second condition?
  3. I understand that ". ~/.bashrc" is meant to have the shell source that rc, but how in the world does that command accomplish this? What is the "." supposed to indicate here?
  1. -f is a predicate that tests to see if a particular file (~/.bashrc in this case) exists.

  2. ##*/ as part of a variable substitution means remove something ending in "/" from the beginning of the variable's value -- for example, if SHELL were "/usr/local/bin/bash", it'd remove the "/usr/local/bin/" and just give "bash". In detail: ## means remove the longest match possible of what follows, */ matches anything ending in "/".

    Other related options: # means remove the shortest match (i.e. #*/ would remove through the first "/"), %% means remove the longest possible match from the end, and % means remove the shortest match from the end.

  3. . is a shorter name for the source command -- a shell built-in command that executes the contents of a script (~/.bashrc in this case) in the current shell. Normally, if you run a shell script, it's run in a subshell and any variables, aliases, functions etc that it defines are lost when it exits; by sourceing it instead, things it defines are valid in the current shell.


1) checks to make sure the file ~/.bashrc exists

2) ${SHELL} is a variable that contains the full path of the current shell. If you type echo $SHELL or echo ${SHELL} at a terminal, it will print the path. ${SHELL##*/} removes the path and just prints the file name. Type echo ${SHELL##*/} in a terminal to see.

3) the period "." is a built-in bash command to run a script in the current process. I.E. it sources a file.

So that line basically says, "if the file ~/.bashrc exists and the current shell is a bash shell, then source ~/.bashrc"

My ~/.bash_profile just contains:

source $HOME/.bashrc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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