This is the 12th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021.

shell

Confirm the Shell type used by the tty of the current terminal:



Shell(Unix Shell)Is a command line interpreter, yesUnixOperating system under the most traditional man-machine interface.ShellScripts are interpreted, do not need to be compiled, and are similar to most programming languages, with basic variable and flow control statements. We usually useShellThere are two ways:

  • Enter commands, execute them, and this is called interactive (Interactive);
  • Batch (Batch), written in advance by the userShellScript file, and then execute the commands in the script sequentially.

The first Shell environment was the Thompson Shell, developed at Bell LABS and released in 1971. The most prominent ancestor of the modern Shell is the BourneShell, known as SH, named after its founder, Stephen Bourne, who worked at AT&T. Shells have been evolving around this concept by adding new features. For example, early versions of OS X used:

  • tcshBy defaultShell. This is bycsh(C shell), a kind of similarCThe language of theShellEvolved from.

After OS X 10.3 and 10.4, the default Shell is:

  • bashBy theGNUDevelopment.

In addition to the default bash, the default Shell is now ZSH on macOS. It was developed by Paul Falstad in 1990. It is a Bourne Shell that uses the bash and Previous Shell features and adds more features:

  • Spell checker
  • Built-in programming features
  • Friendly interaction



At the same time,macOSThere are many other kinds availableShell:

.bashrc,.bash_profileand.zshrcFunctions and differences

When using command line tools, we may encounter tutorials that require you to write configuration to.bashrc,.bash_profile, or.zshrc. So what are the functions and differences of these documents? Bashrc and.bash_profile are intended for Bash. ZSHRC is for ZSH.

Interactive login and non-loginShell

When the Shell is called, the Shell reads information from a set of startup files and executes commands. What files are read depends on whether the Shell is called as an interactive login or non-login.

In other words, shells are either interactive or non-interactive:

  • interactiveShellIs read and written to the user terminalShellProcedure, the user enters the command on the terminal, and executes immediately after the enter.
  • non-interactiveShellIs terminal independentShellA program, such as when executing a script.

interactiveShellIt could be loginShell, can also be non-loginShell. When the user passessshOr when you log in to the terminal remotely--loginWhen the option is started, login is invokedshell.

When called as an interactive login Shell, Bash looks for the /etc/profile file and, if it exists, runs the commands listed in it. Then, search for ~/.bash_profile, ~/.bash_login, and ~/.profile files, reading them sequentially.

whenBashAs interactive non-loginshellWhen called, it reads~/.bashrc.

So the difference between.bashrc and.bash_profile is that.bash_profile is read and executed when Bash is called as an interactive login shell, while.bashrc is executed for an interactive non-login shell.

Most Linux/Unix distributions use ~/.profile instead of ~/.bash_profile. ~/.profile is read by all shells, whereas ~/.bash_profile is read only by Bash. ~/.zshrc is the user configuration for ZSH’s interactive shell.

For Bash, they work as follows:

  • Read the appropriate content and executeAAnd then executeBAnd then executeCAnd so on.B1.B2.B3Indicates that only the first of those files found is executed.
+----------------+-----------+-----------+------+ | |Interactive|Interactive|Script| | |login |non-login | | +----------------+-----------+-----------+------+ |/etc/profile | A | | | +----------------+-----------+-----------+------+ |/etc/bash.bashrc| | A | | +----------------+-----------+-----------+------+ |~/.bashrc | | B | | +----------------+-----------+-----------+------+  |~/.bash_profile | B1 | | | +----------------+-----------+-----------+------+ |~/.bash_login | B2 | | | +----------------+-----------+-----------+------+ |~/.profile | B3 | | | +----------------+-----------+-----------+------+ |BASH_ENV | | | A | +----------------+-----------+-----------+------+ | | | | | +----------------+-----------+-----------+------+ | | | | | +----------------+-----------+-----------+------+ |~/.bash_logout | C | | | +----------------+-----------+-----------+------+Copy the code

forzsh, they work as follows:

  • Read the appropriate content and executeAAnd then executeBAnd then executeCAnd so on.
+----------------+-----------+-----------+------+ | |Interactive|Interactive|Script| | |login |non-login | | +----------------+-----------+-----------+------+ |/etc/zshenv | A | A | A | +----------------+-----------+-----------+------+ |~/.zshenv | B | B | B | +----------------+-----------+-----------+------+ |/etc/zprofile | C | | | +----------------+-----------+-----------+------+ |~/.zprofile | D | | | +----------------+-----------+-----------+------+ |/etc/zshrc | E | C | | +----------------+-----------+-----------+------+ |~/.zshrc | F | D | | +----------------+-----------+-----------+------+ |/etc/zlogin | G | | | +----------------+-----------+-----------+------+ |~/.zlogin | H | | | +----------------+-----------+-----------+------+  | | | | | +----------------+-----------+-----------+------+ | | | | | +----------------+-----------+-----------+------+  |~/.zlogout | I | | | +----------------+-----------+-----------+------+ |/etc/zlogout | J | | | +----------------+-----------+-----------+------+Copy the code

Verify whether you are currently logged in or notshell

inttyPerformed in theecho $0And the output ofShellIf the front band-“, indicating loginShell.

Configuration recommendations

  1. bash:
  • Put the configuration options in~/.bashrcIn, then in~/.bash_profileThrough thesourceThe call.
  1. ZSH ` :
  • It is recommended to keep the configuration options in~/.bashrc.~/.bash_profileThrough thesourceCall, and finally in~/.zshrcIn thesourcecall~/.bash_profile.