Configuration Method of Bash Environment Variable in Linux

  • 2021-07-26 09:13:44
  • OfStack

Shell is a program written in C language, which is a bridge for users to use Linux. Shell is both a command language and a programming language.

The following are several versions of shell, and bash is the default:

sh (full name Bourne Shell): It is the shell originally used by UNIX and can be used on every UNIX.
Bourne Shell is quite good at shell programming, but not as good at dealing with user interaction as several other shell.
bash (full name Bourne Again Shell): The default of LinuxOS, which is an extension of Bourne Shell.
It is fully compatible with Bourne Shell and adds many features to Bourne Shell. It can provide command completion, command editing and command history. It also incorporates many of the advantages of C Shell and Korn Shell, with a flexible and powerful editing interface and a friendly user interface.
csh (full name C Shell): Shell is a more suitable variant than Bourne Shell, and its syntax is very similar to C language.
Tcsh: Is an extended version of C Shell provided by Linux.
Tcsh includes command-line editing, programmable word completion, spelling correction, historical command replacement, job control and syntax similar to C language. It is not only compatible with Bash Shell prompt, but also provides more prompt parameters than Bash Shell.
ksh (full name Korn Shell): Combines the advantages of C Shell and Bourne Shell and is fully compatible with Bourne Shell.
pdksh: Is an extension of ksh provided by the Linux system.
pdksh supports character control and can suspend, background execute, wake up or terminate programs on the command line.

Let's look at the configuration of Bash environment variable in Linux

In Linux, we generally configure environment variable information into different files, and the commonly used configuration files are

/etc/profile /etc/bashrc ~/.bash_profile ~/.bashrc ~/.bash _logout

The above configurations are mainly different between interactive login Shell and interactive non-login Shell, and different configurations will be loaded.

Interactive login Shell

It is executed after logging in to Linux system and you enter your username and password, or su -l. User name mode

Non-login Shell

After you enter the system, open a terminal Bash to execute.

/etc/profile

1 is the system configuration, which sets the environment information for each user of the system. When the user logs in for the first time, the file is executed and is derived from the /etc/profile.d Collect the settings of shell from the configuration file of the directory.

Right /etc/profile The modification of must be restarted to take effect, and it is valid for every user.

/etc/profile.d/

It can be understood as /etc/profile Part 1, but you can split the configuration into several files according to categories or functions, which is clearer and easier to maintain.

/etc/bashrc

This file is executed for each user running Bash Shell, which is executed when Bash Shell is opened, and its configuration is valid for each Bash opened by all users using bash. When modified, you only need to open a new Bash without restarting.

~/.bash_profile

The file is in the user directory, setting exclusive environment information and starting program for the current user. When the user logs in, the file is executed once, and the current user's .bashrc Files. If there are modifications, they need to be restarted to take effect.

~/.bashrc

Set exclusive Bash information for the current user, which is executed each time a new Shell is opened. If there is any modification, it is only necessary to open a new Shell terminal without restarting.

~/.bash_logout

This file is executed each time the current user exits Bash Shell.

The above files that need to be restarted to take effect are passed through similar files source ~/.bash_profile This method takes effect temporarily, but it doesn't really restart the computer.

Summarize


Related articles: