Types of Bash (interaction, login) and their associated configuration files

Source: Internet
Author: User

Recently, learning about bash, you learned about Bash's basic classification and its associated configuration files. When referring to some information on the internet, it was found that some of the information on the Internet was confusing and not comprehensive, and that the definition of shell classification was not essential, even some of which were misleading readers. Simply summarize yourself according to the relevant content in the Bash manual page.

Bash Usage Overview:

bash [option] File

Bash section option description (only relevant to this article):

-C String If you use the-C option, the command is read from string . If there are arguments behind the string (the parameter arguments of the directive), they will be assigned to the position parameter (bash's parameter parameters) starting with the $.

-I if you use the-i option, the shell is interactive.

-L causes Bash to behave as if it were a login shell (in fact there is no real user to log in, the text here uses the subjunctive tone of the completion tense, the editor's note).

--rcfile file If the shell is interactive, execute the command from file instead of the standard personal initialization file ~/.BASHRC.

--login equivalent to-l

--noprofile does not read the system global boot file/etc/profile, nor does it read any of the following personal initialization files ~/.bash_profile,~/.bash_login or ~/.profile. By default, bash reads these files when it is called as a login shell.

--posix modifies the behavior of bash by default from the POSIX standard to match the POSIX standard (POSIX mode).

Bash Call:

The login shell is the first character of the parameter zero is the "-" number, or the shell started with the--login option. (Note that the shell's special variable 0 is displayed in the login shell, such as Echo $, which will see the recorded character of this variable utilises-beginning, editor's note)

An interactive shell is one that does not have a non-option parameter and the-C option, the standard input and the error are connected to the terminal (determined by Isatty (3)), or a shell that starts with the-i option. If the shell is interactive, PS1 will be set at the same time the variable $-contains the character I, once to implement the use of scripts or startup files to test this state.

The next paragraph describes how bash executes its startup files. If any of the following files exist but cannot be read, bash reports an error. The tilde in the file name is used for expansion, see the extended section of the tilde extension. (It is well known that the "~" extension in fact represents the user's home directory, Editor's note)

When bash is used as an interactive login shell or as a non-interactive shell call with the--login option, bash first reads and executes the command from the file/etc/profile if the file exists. After reading the file, bash searches for the file ~/.bash_profile,~/.bash_login,~/.profile in the following order, then reads the first existing and readable file and executes the commands in it. You can use the--noprofile option to suppress this behavior when the shell starts.

If the login shell is logged off, bash reads and executes the commands in the file ~/.bash_logout and/etc/bash.bash_logout if the file exists.

When you want to start a non-logon interactive shell, bash reads and executes the command from ~/.BASHRC if the file exists. You can use the option--NORC to suppress this situation. The option--rcfile file forces Bash to read from the file and execute the command, rather than from ~/.BASHRC.

When bash starts non-interactively, such as running a shell script, bash looks for the variable bash_env in the environment, and if bash_env exists, bash expands its value and uses the value of that variable as the name of the file to read and execute. Bash behaves as if the following command executed the result:

If [-N "$BASH _env"]; Then.    "$BASH _env"; Fi

However, the value in the PATH variable is not used to search for the file name. (If you want to use the variable bash_env to specify the bash's startup file, note that the full path of the file is used, the editor's note)

If called with the name SH, Bash tries to simulate the start-up behavior of the SH history version as closely as possible, and also to conform to the POSIX standard. When used as an interactive login shell or as a non-interactive shell call with the--login option, bash first attempts to read and execute commands from/etc/profile and ~/.profile sequentially. You can use the option--noprofile to prohibit this behavior. When invoked as an interactive shell with the name SH, bash looks for the variable env, which expands its value if env has been defined, and uses the extended value as the name of the read and execute file. Since the shell that is called with the SH method does not read any other startup files and executes commands from it, the--rcfile does not have an impact. Non-interactive shells invoked with the SH name will not read any other files. When called with the SH mode, Bash enters POSIX mode after reading the boot file.

When bash enters POSIX mode, such as using the--posix command-line option, Bash's handling of boot files follows the POSIX standard. In this mode, the interactive shell expands the env variable, and the command is read and executed from a file with the variable extension value as its name. Bash will no longer read other startup files.

When run, Bash tries to decide by remote shell Daemon, usually rshd, or secure Shell daemon sshd, to link to standard input on a network connection. If bash decides to run this way, it will read and execute the command from ~/.BASHRC if the file exists and is readable. If called by SH, bash will not handle this. Option--norc


This article is from "GE Niu qiaoshanzhenhu" blog, please make sure to keep this source http://geshandaniu.blog.51cto.com/6567097/1663840

Types of Bash (interaction, login) and their associated configuration files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.