Bash configuration file (Linux learning eight)

Source: Internet
Author: User
Tags posix

First, what is the shell

The generalized shell means that the interface that can manipulate the application becomes the shell, including the GUI of Linux and Windows.

The narrow shell refers to the command-line aspect of the program, including ZSH,BASH,CSH, etc.

View the available shells for the current system, the shell listed in file/etc/shells, known as a secure Shell list on the current system. The default shell, if it is not a shell in file/etc/shells, is likely to refuse to log in to the system.

[Email protected] ~]# cat/etc/shells/bin/sh/bin/bash/sbin/nologin/usr/bin/sh/usr/bin/bash/usr/sbin/nologin/bin/ Tcsh/bin/csh

The list of shells in the/etc/shells file is called a legitimate shell list because some of the system's services are running, checking the shells that the user can use, and the standard of checking is based on the list of shells in the/etc/shells file.

The difference between the Bash command and the SH command

The default shell of the Linux operating system is the Bourne Again shell, which is an extension of the Bourne shell, called Bash, completely backwards compatible with the Bourne shell, and adds and enhances many features on the basis of the Bourne shell.

/bin/sh in the Gnu/linux operating system is a symbolic link to bash (Bourne-again Shell), but given that bash is too complex, someone transplanted ash from NetBSD to Linux and renamed it dash (Debian ALM Quist Shell), and it is recommended to point/bin/sh to it for faster script execution. The Dash shell is much smaller than the Bash shell and is POSIX compliant.

In Debian and Ubuntu,/bin/sh is already pointing to dash, which is a different shell from bash, which is mostly for scripting, not interactive, it's faster, but it's much less powerful than bash, and the syntax adheres to the POSIX standard.

Iii. Four modes of bash

In the invocation section of Man Bash, the four modes of bash are described, and bash chooses to load different profiles based on these four modes, and the order of loading varies.

Bash is a shell in which a running bash has two properties (State/mode), one that interactive the shell (interactive shell) and the other, whether the login shell (login shell). The combination of the two is four modes.

    • Login shell (interactive shell):

definition : A login shell is one whose first character of argument Zero are A-, or one started with the--login option.

It probably means : a login shell, the first character of its 0th argument is-or it's a shell that starts with the--login option.

explain this definition :

1. The first character of the 0th parameter is-the shell is a login shell

For example: Tty1-tty6 Terminal login System (System default shell is bash) is a log-in shell

Tty1-tty6 log in to the system

Echo $0-bash

The first character of the 0th parameter is--so this is a login shell.

2. Bash launched with the--login option is a log-in shell

For example: Bash--login[-l]

Once the system is logged in, the child shell is logged into the current shell, plus the--login option, which means that the login shell

In addition In addition to the definition, we judge the methods of these two patterns.

The difference between a login shell and a non-login shell we can literally understand that the login shell requires the user to enter a username and password. Contains the entire process of logging into a shell. (This is not necessarily rigorous and accurate, such as Bash--login[-L] This way does not need to enter the user name password, because it is logged into the current shell of the child shell, user name, password and the current shell is the same, so omitted)

The exit mechanism for both the login shell and the non-login shell is different.

Non-login shell, exit using Exit command, cannot exit with logout command

Login shell, when exiting, use the logout command to exit, can use the exit command to exit, but the exit command at this time to throw is called the logout command.

Common login Shell vs. No-login Shell

The following is a login shell statistic is incomplete. (No-loginshell and instructions to be updated)


Su-[l] UserName

SSH [email protected]

SSH [email protected] "command"


Su-[l] username-c "command"

Bash--login[-l]-C "command"


    • Interactive shell (log-in shell):

definition : An interactive shell was one started without non-option arguments and without the-c option whose standard in  Put and error is both connected to terminals (as determined by Isatty (3)), or one started with THE-I option. PS1 is set and $-includes I if bash are interactive, allowing a shell script or a startup file to test the state.

(Explanations and common non-interactive shells to be updated )

Four, mode profile file loading process

The role of the configuration file: Persist the user's configuration and read only once at login

There are several combinations of shell permutations in operation :

    • Sign in to the interactive shell

    • Sign in to a non-interactive shell

In both modes, the order and process of loading the configuration files are the same

The description in the man Bash Help document is as follows:

When Bash was invoked as an interactive login shell, or as a non-interactive shell with the--login option, it first Rea  DS and executes commands from the File/etc/profile, if the file exists. After reading. File, it looks for ~/.bash_profile, ~/.bash_login, And~/.profile, on that order, and reads and EXE  Cutes commands from the first one, the exists and is readable. The--noprofile option may be used when the shell was started to inhibit this behavior.

When a login shell exits, bash reads and executes commands from the Files~/.bash_logout and/etc/bash.bash_logout, if the Files exists.

    • Non-Login Interactive shell

When the an interactive shell-is-not-a login shell is started, bash reads and executes commands from ~/.BASHRC, if- File exists.  This May is inhibited by using the--NORC option. The--rcfile file option would force bash to read and execute commands from file instead of ~/.BASHRC.

    • Non-logon non-interactive shell

When Bash was started non-interactively, to run a shell script, for example, it looks for the variable bash_env in th e environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and E  Xecute. Bash behaves as if the following command were executed:

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

But the value of the the PATH variable was not used to search for the file name.

(English explanation to be continued , non-login non-interactive shell load bash_env variable test process a bit of a problem, the problem link to be continued . Instructions for each configuration file are not continued )

v. Add that if the command command is run with another user, whether the add-or-l option causes the environment variable to load unsuccessfully

The following is a long time ago online search, there is no original article link.

sudo command

the sudo command is a Root's identity execution command , but the environment variable is also the current user, and the execution directory is still the current directory

that environment variables and execution directories are not switched to root

sudo Command1 | Command2 This command will only be Command1 root, but command after the pipe is still not rooted. Sudo will only elevate the permissions of the command immediately following it.

Su-username-c "Command1;command2"

The su command is a switch to another user, The environment variable is switched to username, and the execution directory switches to the home directory of the target user username


Assuming that the current user is a normal user lx (the user does not oracle_sid the environment variable), execute the command with an LX user

Su-oracle-c "Echo $ORACLE _sid"

The output will be empty

SU-USERNAME-C environment variable will switch to username, why not print out the ORACLE_SID environment variable of Oracle user?

Because the double quotation marks are not masked $ this special character, in the execution su-oracle-c "echo $ORACLE _sid" command,

The variable oracle_sid is replaced under the current user (the current user's oracle_sid variable is empty), and the command sent to Oracle executes the echo ""

Our intention is to send the echo $ORACLE _sid This command to the Oracle user and print out the environment variable ORACLE_SID for the Oracle user user, with the following workaround:

1. Su-oracle-c ' echo $ORACLE _sid ' (single quotes will mask all special characters)

2, su-oracle-c "echo \ $ORACLE _sid"

Command-line command "

Su and Su-the command does not:

su Command Switch User identities only , for example, switch from A to B, execute the WHOAMI command, display User B, but the current directory does not switch,

environment variable is still not switched , still a user's environment variable

Su-command switch user, a switch to B, will be executed in the process of login B , will not only switch users , the. Profile file is also executed,

Change to B user's environment variable, directory switch to B's home directory

Articles to be updated

Notebook is too bad, virtual machine can not run, Saturday Lantern Festival

Back to the company some examples to update

Reference: Man bash's help documentation.

This article is from the "Xiaoliuer" blog, make sure to keep this source

Bash configuration file (Linux learning eight)

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: 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.