There are a number of hidden files tucked away in your home directory. On macOS, you’ll find a file named “.bash_profile” up near the top of your hidden files. The bash_profile file can be used to customize and control your Terminal commands, adding shortcuts and adjusting how commands are run. If you’ve used Linux, it’s almost identical to the bashrc file, which does essentially the same thing.
What is bash_profile?
On macOS, bash is installed as your default shell. The name is actually an acronym, which stands for Bourne Again Shell. Bash is one of the most common terminal shells in use today. As a shell, bash interprets your typed input to the Terminal program and runs commands based on your input. Bash allows for some degree of customization using scripting, which is where bash_profile comes in.
In order to load your preferences, bash runs the contents of the bash_profile file at each launch. This shell script is found in each user’s home directory. It’s used to save and load your terminal preferences and environmental variables.
Terminal preferences can contain a number of different things. Most commonly, the bash_profile file contains aliases that the user always wants available, as well as the user’s PATH. Aliases allow the user to refer to commands by shorter or alternative names, and can be a huge time-saver for those that work in a terminal regularly. The PATH tells the shell where to look for typed commands.
How can I edit bash_profile?
You can edit bash_profile in any terminal text editor. We will use nano in the following examples.
To edit bash_profile using nano, invoke the following command in Terminal:
nano ~/.bash_profile
If you’ve never edited your bash_profile file before, you might find that it’s empty. That’s fine! If not, you can feel free to put your additions on any line.
It’s also possible that you might not have a bash_profile file. If so, opening the file in nano will create it automatically.
Any changes you make to bash_profile will be applied next time you launch terminal. If you want to apply them immediately, run the command below:
source ~/.bash_profile
You can add to bash_profile where ever you like, but feel free to use command (proceeded by #) to organize your code.
Edits in bash_profile have to follow bash’s scripting format. If you don’t know how to script with bash, there are a number of resources you can use online. You can find a fairly comprehensive introduction into the aspects of bashrc (similar to bash_profile) that we couldn’t mention here.
Why should I edit bash_profile?
There are a couple of useful tricks you can do to make your terminal experience more efficient and user-friendly.
Add to your PATH
The PATH is an environmental variable that tells your shell where to look to execute text files. By default, it contains several directories. The order of directories matters, since that’s the order in which the shell with search for matching commands. So, if you have a specific version of a command stored earlier in your path, that’s the one that will get executed.
If you need to append a directory your PATH, you’ll be told why. For example, if you’re installing Python 3 and want it to take over the python
command, you can put its executable directory earlier in your PATH than the default version of Python.
You can also add a directory to your PATH to make running your own scripts easier. Simply append the appropriate directory at the end of the export PATH
command, placing a colon between the end of the line and the fully qualified path to your personal script folder.
Aliases
Aliases can also allow you to access a favored form of a command with a shorthand code. Let’s take the command ls as an example. By default, ls displays the contents of your directory. That’s useful, but it’s often more useful to know more about the directory, or know the hidden contents of the directory. As such, a common alias is ll
, which is set to run ls -lha
or something similar. That will display the most details about files, revealing hidden files and showing file sizes in “human readable” units instead of blocks.
You’ll need to format your aliases like so:
alias ll="ls -lha"
Type the text you want to replace on the left, and the command on the right between quotes. You can use to this to create shorter versions of command, guard against common typos, or force a command to always run with your favored flags. You can also circumvent annoying or easy-to-forget syntax with your own preferred shorthand.
Functions
In addition to shorthand command names, you can combine multiple commands into a single operation using bash functions. They can get pretty complicated, but they generally follow this syntax:
function_name () { command_1 command_2 }
The command below combines mkdir
and cd
. Typing md folder_name
creates a directory named “folder_name” in your working directory and navigates into it immediately.
md () { mkdir -p $1 cd $1 }
The $1
you see in the function represents the first argument, which is the text you type immediately after the function name.
Conclusion
Unlike some terminal customization tricks, editing bash_profile is fairly straight-forward and low risk. If you mess anything up, you can always delete the bash_profile file completely and replace it will the default version, or an empty text file.
You might also like the following posts: