Linux environment variables

  
 

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, including 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 to output:
$TEST="Linux Programming"$echo $TESTLinux Programming

Note: Variable assignment cannot be added before $ symbol, the variable must be prefixed with $. When you exit the shell, the variables will disappear.

After logging in to the system, the shell will have an initialization process for setting environment variables. At this stage, Shell will read the /etc/profile and .profile files as follows:

  • Shell first checks if the /etc/profile file exists, and if it exists, reads the content, otherwise Just skip, but no error.
  • Then check if there is a .profile file in your home directory (login directory), if it exists, read the content, otherwise skip it and will not report an error.

    After reading the above two files, the shell will appear. $ Command Prompt:
    $

    When this prompt appears, you can enter the command and call the corresponding program.

    Note: The above is the initialization process of the Bourne Shell. bash and ksh also check other files during the initialization process.

    .profile file

    The /etc/profile file contains general shell initialization information, maintained by the Linux administrator, and the general user has no right to modify it.

    But you can modify the .profile file in the home directory and add some "private customization" initialization information, including:

  • Set the default terminal type and appearance style;
  • Set the shell command to find the path, which is the PATH variable;
  • Set the command prompt.

    Find the .profile file in your home directory and open and view the content using the vi editor.

    Set Terminal Type

    In general, the terminal we use is set by the login or getty program, which may not meet our habits.

    For terminals that have not been used, it may be unfamiliar, not accustomed to the command output style, and it is a little difficult to interact. Therefore, the average user will set the terminal to the following type:
    $TERM=vt100$

    vt100 is an abbreviation for virtual terminate 100. A virtual terminal is a fake terminal. A terminal that has its own display and keyboard will be connected to the host computer through a special cable (such as a serial port). Vt100 is a virtual terminal specification supported by most Linux systems. Also commonly used are ansi, xterm, etc.

    Set PATH variable

    When you enter a command at the command prompt, the shell will look up the program corresponding to the command according to the PATH variable. The PATH variable indicates the path where the program is located.

    In general, the PATH variable is set as follows:
    $PATH=/bin:/usr/bin$

    Multiple paths are separated by a colon (:). If the command entered by the user is not found in the path set by PATH, it will report an error, for example:
    $hellohello: not found$

    PS1 and PS2 variables

    PS1 variable is used to save the command prompt , you can modify it at will, if you are not used to using $ as a prompt, you can also change to other characters. After the PS1 variable is modified, the prompt changes immediately.

    For example, set the command prompt to ’=>’:
    $PS1='=>'=>=>=>

    You can also prompt Set to the current directory, for example:
    =>PS1="[\\u@\\h \\w]\\$"[root@ip-72-167-112-17 /var/www/tutorialspoint/Linux ]$[root@ip-72-167-112-17 /var/www/tutorialspoint/Linux]$

    The command prompt contains the username, hostname, and current directory.

    The escape characters in the table below can be used as parameters for PS1 to enrich the command prompt information.

    escape character
    description
    \\t current time, format is HH:MM:SS \\d current date, format is Weekday Month Date \ newline\\W current Where the directory \\w is the full path to the current directory\\u username\\h hostname (IP address) # Enter the number of commands, each time you enter a new command, add 1 \\$ if it is superuser root, prompt Is #, otherwise $.

    You can change 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 in.

    If the command entered by the user is incomplete, the shell will also use the second prompt to wait for the user to complete the input of the command. The default second command prompt is >, which is saved in the PS2 variable and can be modified at will.

    The following example uses the default second command prompt:
    $ echo "this is a> test"this is atest$

    The following example changes the prompt via the PS2 variable:
    $PS2="secondary prompt->"$ echo "this is asecondary prompt->test"this is atest$

    Common Environment Variables

    The following table lists some of the important Environment variables, which can be modified in the manner mentioned above.

    Variables
    Description
    DISPLAY is used to set where the graphic is displayed. HOME The home directory of the current user. IFS internal domain separator. LANG LANG allows the system to support multiple languages. For example, setting LANG to pt_BR will support (Brazil) Portuguese. PATH specifies the path to the shell command. The directory where PWD is currently located, 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. It can be AST (Atlantic Standard Time) or GMT (Greenwich Mean Time). UID The current user ID, expressed in numeric form, is initialized when the shell is started.

    The following example uses some environment variables:
    $ echo $HOME/root]$ echo $DISPLAY$ echo $TERMxterm$ echo $PATH/usr/local/bin:/bin:/usr /bin:/home/amrood/bin:/usr/local/bin$

  • Copyright © Windows knowledge All Rights Reserved