This is vacation time with a holiday in the Netherlands. Posting will be more sporadic than usual.
There are (mainly) three customizations you can perform:
This post will look at some useful environment variables.
Choose your own
PATH environment variable contains a list of directories that
bash will search through for commands. You can show your current path by
$ echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:
This is the default
PATH for a ‘clean’ macOS. Depending on what you have installed, there may be more elements in your
PATH. The individual directories are separated by a colon
:. When you enter a command, like for example
systemsetup at the prompt,
bash will search through the directories in
PATH in the order until it finds a match.
Note: if there is ever any question on which command is actually executed, you can use the
which command to get the effective path to a given command from the shell:
$ which python /usr/bin/python $ which systemsetup /usr/sbin/systemsetup $ which bash /bin/bash
You might have installed other (newer versions) of certain tools (such as
bash itself). Depending on how you install them they may be installed in a way that overrides the tool that is part of macOS. This may lead to problems when scripts assume the system version of a tool.
You can add your own directories by either appending (safe) or prepending (risky) your own directories to the
PATH=$PATH:~/bin export PATH
This will add the
~/bin directory (which does not exist by default, you have to create and fill your own) to your
export command tells
bash it should export this variable to child processes.
You can add more than one directory to the
PATH, either at once, or one per line:
PATH=$PATH:~/bin PATH=$PATH:~/Projects/scripts export PATH
bash stops looking when it finds a match, the order of the directories in the
PATH is important. When you place your directories before the default directories you can override some of the system commands. This can be powerful, but also very dangerous. There are safer methods for overriding system commands. You should add your directory to the end of the
PATH to be safe.
PATH (and other environment variables) are exported to child-shells and scripts executed from the shell. However, not every tool executes directly from a shell, or from a shell with your configuration file. So you should never assume
PATH (or any other environment variable) is set to anything specific (or even set) when you write a script. You can either set the
PATH at the beginning of a script, or use absolute paths for commands in scripts such as
/usr/bin/pkgbuild. You can determine the path to a command with the
You can also configure the command prompt, i.e. the text that
bash displays before you enter a command. By default the macOS prompt looks like this:
The configuration for the prompt is stored in the
PS1 environment variable. You can see the default value by
$ echo $PS1 \h:\W \u\$
The letters with the backslash
\ in the prompt variable are placeholders that will be replaced with a specific value when the prompt is printed out.
\hwill be replaced with the hostname of the computer up to the first
\Hwould be the complete hostname)
\Wwill be the basename of the current working directory
\uis the current user
$unless the current user is
rootor effectively elevated to
rootprivileges, in which case it will be
#. This is very useful as a warning when working with elevated privileges.
Since I am the only user on my Mac, and I also know which Mac I am sitting at, I have changed my prompt to something simpler:
export PS1="\W \$ "
Note the space after the
\$. If you forget that the cursor will stick right on the
(When I log in to a different Mac with ssh the different prompt also reminds me that this shell is on a different Mac.)
You can find a list of escape sequences here.
You can also add color to the prompt data, but the escape sequences will look very messy very quickly. To have the directory name in gray, you use:
export PS1="\[\e[1;30m\]\W\[\e[m\] \\$ "
List in Color
You can also tell
bash to color for some other commands, such as
ls. To do that you just have to set the
Change the Editor
Some terminal commands will open a text editor directly. For example
git may open a text editor so you can enter a commit message. By default the system will open
vi but you can change the editor that will be used. There are even some graphical editors that can be used this way. The environment variable for this behavior is
export EDITOR=vi export EDITOR=vim
export EDITOR=nano export EDITOR=pico
export EDITOR="open -f"
export EDITOR="bbedit -w --resume"
export EDITOR="mate -w"
export EDITOR="subl -n -w"
export EDITOR="atom --wait"
--wait argument tells the UI application that there is another process waiting for their output.
Putting all of this together, you can add these lines to your `.bash_profile` or `.bashrc`:
In the next post we will look at
bash aliases and functions.