In Linux, environment variables are a very important concept. environment variables can be set by the system, user, Shell, and other programs.
A variable is a string that can be assigned a value that includes numbers, text, file names, devices, and other types of data.
In the following example, we will assign a value to the variable TEST and then use the echo command output:
$TEST = "Linux Programming" $echo $TESTLinux programming
Note: variable assignments cannot be preceded by a $ sign, and a $ prefix must be added to the output of the variable. When you exit the Shell, the variable disappears.
After logging into the system, the shell will have an initialization process to set the environment variables. At this stage, the shell reads the/etc/profile and. Profile two files, the process is as follows:
- The shell first checks to see if the/etc/profile file exists and, if it exists, reads the content, or skips it, but does not error.
- Then check to see if there is a. profile file in your home directory (login directory), and if it exists, read the content, or skip it and not get an error.
After reading the above two files, the shell will appear at a command prompt:
$
At this prompt, you can enter a command and invoke the appropriate program.
Note: The above is the initialization process for the Bourne Shell, and bash and ksh also check other files during initialization.
. profile file
The/etc/profile file contains generic shell initialization information, which is maintained by the Linux administrator and is not authorized by the general user to modify.
But you can modify the. profile file in the home directory to add some "private custom" initialization information, including:
- Set the default terminal type and appearance style;
- Set the shell command lookup path, which is the path variable;
- Sets the command prompt.
Locate the. profile file in the home directory and use the VI Editor to open and view the content.
Set the terminal type
In general, the terminal we use is set by the login or Getty program and may not conform to our custom.
For unused terminals, may be unfamiliar, not accustomed to command output style, interactive slightly difficult. Therefore, the general user will set the terminal to the following type:
$TERM =vt100$
VT100 is the abbreviation for virtual terminate 100 . Virtual terminal is a kind of fake terminal, the terminal that really has its own monitor and keyboard, will connect to the computer host through special cable (such as serial port). VT100 is a virtual terminal specification supported by most Linux systems, and it is commonly used for ANSI,xterm and so on.
Set the PATH variable
when you enter a command at a command prompt, the shell looks for the program that corresponds to the command based on the path variable, which indicates the path to which the program is located.
In general, the path variable is set as follows:
$PATH =/bin:/usr/bin$
multiple paths are delimited with colons (:) . If the command entered by the user is not found in path settings, an error will be provided, for example:
$hellohello: Not found$
PS1 and PS2 variables
The PS1 variable is used to save the command prompt and can be modified at will, if you are not accustomed to using $ as a prompt, you can change it to another character. The prompt changes immediately after the PS1 variable is modified.
For example, set the command prompt to ' = ':
$PS 1= ' = ' =>=>=>
You can also set the prompt information to the current directory , for example:
= =ps1= "[\[email protected]\h \w]\$"[[email protected]/var/www/tutorialspoint/linux]$[[email protected] /var/www/tutorialspoint/linux]$
The command prompt contains the user name, host name, and current directory.
The escape characters in the following table can be used as PS1 parameters to enrich the command prompt information.
Escape Character |
Description |
\ t |
Current time, formatted as HH:MM:SS |
\d |
Current date, formatted as weekday Month date |
\ n |
Line break |
\w |
Current directory |
\w |
The full path to the current directory |
\u |
User name |
\h |
Host name (IP address) |
# |
Enter the number of commands, each input a new command will be added 1 |
\$ |
If Root is Superuser, the prompt is #, otherwise $. |
"\s-\v\$", which shows the version of the Shell's name
You can modify the prompt each time you log in, or you can add the PS1 variable to the. profile file so that the prompt is automatically modified each time you log on.
If the user enters a command that is incomplete, theShell also uses a second prompt to wait for the user to complete the input of the command. The default second command prompt is, save in the PS2 variable, can be arbitrarily modified .
A very long command can be displayed by adding "\" to its branch at the end. The default prompt for multi-line commands is ">"
The following example uses the default second command prompt:
$ echo "This was a> test" this is atest$
The following example changes the prompt through the PS2 variable:
$ ps2= "secondary prompt->" $ echo "This was Asecondary prompt->test" This is atest$
Common environment variables
The following table lists some of the important environment variables that can be modified in the way mentioned above.
variable |
description |
DISPLAY |
is used to set where the graphic will be displayed. |
home |
home directory for the current user. |
ifs |
internal domain delimiter. |
lang |
Lang allows the system to support multiple languages. For example, if you set Lang to PT_BR, you can support (Brazilian) Portuguese. |
path |
Specifies the path to the shell command. |
PWD |
current directory, that is, the directory to which  CD is located. |
random |
generates a random number between 0 and 32767. |
term |
sets the terminal type. |
tz |
time zone. This can be AST (Atlantic Standard Time) or GMT (Greenwich Mean Time). |
uid |
is initialized as a digital representation of the current user Id,shell startup. |
Some of the environment variables are used in the following example:
$ echo $HOME/root]$ echo $DISPLAY $ echo $TERMxterm $ echo $PATH/usr/local/bin:/bin:/usr/bin:/home/amrood/bin:/usr/local /bin$
Linux Learning series 5--environment variables