About bash_profile and bashrc on macOS and higher Serbia

前端之家收集整理的这篇文章主要介绍了About bash_profile and bashrc on macOS and higher Serbia前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

About bash_profile and bashrc on macOS

Note:bash_profileis completely different from configuration profiles. Learn more about Configuration Profiles in my book:‘Property Lists,Preferences and Profiles for Apple Administrators’

Note: please consider supporting the author of this siteby purchasing one of my books! Thank you!

In thisspontaneous series on the macOS TerminalI have often mentioned adding something as analiasorfunctionto yourbash_profileorbashrc. ObvIoUsly,you may wonder: how do I do that? And which file should I use?

Why?

When you work with the command line and thebashshell frequently,you will want to customize the environment. This can mean changing environment variables,such as where the shell looks for commands or how the prompt looks,or adding customized commands.

For example,macOS sets thePATHenvironment variable to/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbinby default. This is a list of directories (separated by a colon ‘:’) that the system searches through in order for commands. I like to add a folder in my home directory~/binto that list,so that I can execute certain tools without needing to type out the full path. (e.g.munkipkg,quickpkgandssh-installer).

Inbashyou append to existingPATHdo this with:

export PATH="$PATH:~/bin"

Youcouldtype this command every time you open a new Terminal window (i.e. shell),or you can configure your shell to do this automatically.

Depending on which shell you use and how you start the shell,then certain script files will be executed which allow you to set up these customizations.

This article will talk about customizingbashon macOS. Other shells and other operating systems may have other files or rules.

So,which file?

Thanks to the rich and long history ofbashthe answer to which file you should put your configuration in,is surprisingly confusing.

There are (mainly) two user level files whichbashmay run when abashshell starts.~/.bash_profileor~/.bashrc.

Both these files are on the first level of your home directory~/. Since the file names start with a.Finder and normallswill not show them. You need to usels -ato see if they are present.Read more about invisible and hidden files here.

The usual convention is that.bash_profilewill be executed at login shells,i.e. interactive shells where you login with your user name and password at the beginning. When yousshinto a remote host,it will ask you for user name and password (or some other authentication) to log in,so it is a login shell.

When you open a terminal application,it does not ask for login. You will just get a command prompt. In other versions of Unix or Linux,this will not run the.bash_profilebut a different file.bashrc. The underlying idea is that the.bash_profileshould be run only once when you login,and the.bashrcfor every new interactive shell.

However,Terminal.app on macOS,doesnotfollow this convention. When Terminal.app opens a new window,it will run.bash_profile. Not,as users familiar with other Unix systems would expect,.bashrc.

Note: The Xterm application installed as part ofXquartzruns.bashrcwhen a new window opens,not.bash_profile. Other third-party terminal applications on macOS may follow the precedent set by Terminal.app or not.

This is all very confusing.

There are two main approaches:

  • When you are living mostly or exclusively on macOS and the Terminal.app,you can create a.bash_profile,ignore all the special cases and be happy.
  • If you want to have an approach that is more resilient to other terminal applications and might work (at least partly) across Unix/Linux platforms,put your configuration code in.bashrcand source.bashrcfrom.bash_profilewith the following code in.bash_profile:
if [ -r ~/.bashrc ]; then
   source ~/.bashrc
fi

Theif [ -r ... ]tests wether a file exists and is readable and thesourcecommand reads and evaluates a file in place. Sometimes you see

[ -r ~/.bashrc ] && . ~/.bashrc

(mind the spaces) Which is a shorter way to do the same thing.

Since either file can drastically change your environment,you want to restrict access to just you:

$ chmod 700 ~/.bash_profile
$ chmod 700 ~/.bashrc

That was confusing. Is that all?

No. There are more files which may be executed when a shell is created.

Whenbashcannot find.bash_profileit will look for.bash_loginand if that does not exist either.profile. If.bash_profileis present the succeeding files will be ignored. (though you cansourcethem in your.bash_profile)

There is also a file/etc/profilethat is run for interactive login shells (and Terminal.app). This provides a central location to configure the shells for all users on a system. On macOS/etc/profilesets the defaultPATHwith thepath_helpertool and thensources/etc/bashrcwhich (you guessed) would be the central file for all users that is executed for non-login interactive shells. For macOS Terminal.app/etc/bashrcsets the default prompt and then itself sources/etc/bashrc_Apple_Terminalwhich sets up the session persistence across logins.

So in macOS Terminal.app,before you even see a prompt,these scripts will be run:

  • /etc/profile
    • /etc/bashrc
      • /etc/bashrc_Apple_Terminal
  • if it exists:~/.bash_profile
    • when~/.bash_profiledoes not exists,~/.bash_login
    • when neither~/.bash_profilenor~/.bash_loginexist,~/.profile
  • ~/bash_profilecan optionally source~/.bashrc

There is also a file~/.inputrc,where you can setup certain command line input options. One common example for this isto enable case-insensitive tab-completion. You can finda list of more options here.

Finally,there is~/.bash_logoutwhich is run when a shell exits or closes.

Ok,so I have the file,now what?

Whichever file you choose,(I went with option one and have everything in.bash_profile) now you want to put stuff in it.

Technically this is a script,so you can do anything you can code inbash. However,usually the contents of a.bash_profileor.bashrcfall into one of three categories:

I will show some examples for each in thenext post!

猜你在找的Bash相关文章