Skip to content

Instantly share code, notes, and snippets.

@dotherightthing
Last active October 6, 2021 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotherightthing/359c8ce80a7442521fd24161726923d5 to your computer and use it in GitHub Desktop.
Save dotherightthing/359c8ce80a7442521fd24161726923d5 to your computer and use it in GitHub Desktop.
[Bash Profile] The .bash_profile is loaded before Terminal loads your shell environment. It contains all the startup configuration and preferences for your command line interface. #bash #cli #macos

Bash Profile / PATH

What is Bash?

Bash (the Bourne Again SHell) is an interactive shell, which responds to user typed commands with actions.

Understanding start-up files

When Bash starts up, it executes the commands in a variety of 'dot' files, including ~/.bash_profile.

This file also registers environmental variables, such as PATH, which contains a colon-delimited list of directories that the shell searches through when you enter a command.

The file is just shell commands. It is typically used to change prompts, set environment variables, and define shell procedures. Traditionally, the file .profile is used for this purpose, but bash has so many extensions that it needs its own startup file for users that want to put bashisms in startup files.

If you have both ~/.profile and ~/.bash_profile then the ~/.profile will not be sourced when Terminal.app starts.

Make sure that the file ~/.profile contains the line source ~/.bashrc. If it doesn't, add it to the end of that file, save it, then restart terminal.

This file is loaded before Terminal loads your shell environment and contains all the startup configuration and preferences for your command line interface. Within it you can change your terminal prompt, change the colors of text, add aliases to functions you use all the time, and so much more.

How to add a missing command

When Bash complains that a command can't be found, it is saying that it does not understand the command, because it does not know how to action it.

Alternatively, it might know how to action it, but the linked application is old and lacking the abilities of one which you have since installed yourself.

Adding a binary to the PATH variable associated with your user profile is simple:

  1. Open the .bash_profile configuration file, which Bash loads when it starts up
  2. Register the correct path of the binary or application against the PATH variable, which Bash searches through when actioning commands
  3. Close and save the .bash_profile configuration file, to apply the changes
  4. Activate your changes
  5. Output the $PATH variable, to check that it includes the correct path

1. Open the .bash_profile configuration file, which Bash loads when it starts up

In MacOS command-line terminology, the tilde symbol (~) refers to your home directory, e.g /Users/Joe.

$ nano ~/.bash_profile

2. Register the correct path of the binary or application against the PATH variable, which Bash searches through when actioning commands

You can either add the path to a specific binary, or the path to a folders of binaries.

For example, this points the mysql command to the copy of mysql that ships with MAMP Pro (bash mysql... -> /Applications/MAMP/Library/bin/mysql ...):

export PATH="/Applications/MAMP/Library/bin/mysql"

While adding an entire folder of binaries also exposes other commands, such as mysqladmin, which also ship with MAMP Pro:

export PATH="/Applications/MAMP/Library/bin:$PATH"

Note that running export ... directly in Terminal doesn't update either the .profile nor the .bash_profile (at least in my experience).

3. Close and save the .bash_profile configuration file, to apply the changes

CONTROL+X, Y, ENTER

4. Activate your changes

$ source ~/.bash_profile 

5. Output the $PATH variable, to check that it includes the corrected path

$ echo $PATH

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment