Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for Bash start-up files on a Mac

Tags:

bash

macos

As I understand it, the order that start-up files are read by the bash shell on a Mac are...

  1. ~/.bash_profile
  2. ~/.bash_login
  3. ~/.profile

..and once one file in this list is found, the contents of the other is ignored.

That being said, is there a best practice for which of these files should be my one true Bash start-up file?

On one hand, if .bash_profile will take precedence over any other potential start-up file, then that should be used, because you can be sure that 100% of the time the info in that start-up file is being run.

On the other hand, if .profile is the file that exists on Mac systems by default, and .bash_profile needs to be manually created, then perhaps that should be used, and there will never be a reason to create a .bash_profile file.

Thoughts?

like image 807
Bob. Avatar asked Dec 20 '10 19:12

Bob.


People also ask

What is the startup file in bash?

The shell program /bin/bash (hereafter referred to as “the shell”) uses a collection of startup files to help create an environment to run in. Each file has a specific use and may affect login and interactive environments differently. The files in the /etc directory provide global settings.

Where is .bash_profile file in Mac?

Go the users home directory. Check if the bash_profile is visible there or not. If not, press Command + Shift + . and bash_profile will be visible.

What is bash_profile Mac?

In simple words, bash_profile is a configuration file for the Bash shell, which is a hidden file in your Mac's user directory. The bash profile on Mac has loaded before Terminal loads your shell environment and contains all the startup configuration and preferences for your command-line interface.


1 Answers

It depends on whether you use shells other than bash, and whether you use bash-only features in your profile. If you use other sh-style shells (sh, ksh, zsh, etc but not csh or tcsh), don't use bash-only features and want the same setup no matter what shell you're in, you should use .profile. If you want to use bash-only features, use .bash_profile. If you want to use multiple shells but also use bash-only features, put the common stuff in .profile and the bash-only stuff in .bash_profile, then add if [ -f ~/.profile ]; then . ~/.profile; fi to .bash_profile.

If you only ever use bash, but don't rely on any bash-only features in your profile, then it doesn't really matter.

There's actually another complication: login bash shells source either .bash_profile, .bash_login, or .profile; non-login interactive bash shells (e.g. subshells) source .bashrc instead. I tend to want the same setup in both login and non-login shells, so I put all the interesting stuff in .bashrc, and then if [ -f ~/.bashrc ]; then . ~/.bashrc; fi in .bash_profile. If I also used other shells, I'd probably put most of it in .profile instead, and have .bashrc source that instead.

like image 156
Gordon Davisson Avatar answered Sep 20 '22 22:09

Gordon Davisson