Translated from: Medium.com/zarinlo/de… A section of bash for

Bash profile

An overview of

The Bourne shell, also known as bash, is a shell for UNIX and Linux that also happens to be the default shell for macOS. Bash has two configuration files,.bash_profile and.bashrc. You can put your custom configuration in either file, or create one if it doesn’t exist. So why are there two files? What’s the difference?

.bash_profile is executed for the logged in shell, and.bashrc is executed for the interactive shell that is not logged in

Login and non-login shells

In a *nix (Linux+UNIX) environment, the login shell is the first process that executes under your user name when you log in (through an interactive conversation on the machine or remotely over SSH). The login shell usually reads configuration shell files, such as environment variables. For bash shells, the.bash_profile is executed to configure your shell before the initial command prompt is displayed on the terminal.

However, if you have logged in to your machine and opened a new terminal window,.bashrc will be executed

Why do you need two files

In the * NIx environment, if you want to collect some diagnostic information about your machine, such as memory usage, and you only want to see this information when you log in, you should put this information in.bash_profile

Special case for Mac OS X

Every time a new terminal window is opened, Mac OS X runs the login shell, which executes.bash_profile instead of.bashrc

What needs to be added to the bash file?

The PATH variable

PATH is an environment variable that contains a list of file system paths where the operating system can find programs to run. When you run a program in bash, the operating system looks for the program in each of the paths included in PATH, in turn, and will run the first instance of the program it finds. If no program is found in these paths, the terminal returns a program not found error.

What is your PATH

$ echo $PATH
Copy the code

The first path should be /usr/local/bin, followed by /usr/bin. All programs that are native to the operating system you are using are not managed by the distribution package manager (that is, locally compiled packages); they are located in /usr/local/bin. All programs that need to be globally accessible by other users are stored in /usr/bin. You should not install natively compiled packages into /usr/bin because future distribution upgrades may modify or remove these packages without warning.

If this is not what your PATH variable looks like, then you must correct it in your.bash_profile by adding it to your file:

export PATH=/usr/local/bin:$PATH
Copy the code

This simply appends the /usr/local/bin PATH to the front of the pre-existing PATH variable. Each path is separated by a colon (:). Don’t forget the source file!

The source command reads and executes the commands in the input file. You must do this each time you want to enable changes made to your personal data, such as:

source ~/.bash_profile
Copy the code