09. Detailed explanation of PATH variable, PS1 variable, LANG language variable, position parameter variable and predefined variable

content

1. PATH variable

2. PS1 variable

3. LANG language variables 

4. Positional parameter variables

5. Predefined variables

1. PATH variable

[root@localhost lib]# echo $PATH

/usr/lib64/qt3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

The path of the system search command, the value of the PATH variable is: divided paths, these paths are the paths of the system search command. That is to say, when we enter a command, if the path is not written, the system will go to the path defined by the PATH variable to find whether there is an executable program. If no path is written, a "command not found" error is returned. That is to say, we copy our own script to the path defined by the PATH variable, and we can also run our own script without entering the path.

2. PS1 variable

command prompt settings

Format PS1='[\u@\@ \h \# \W]\$ ' Output [root@22:52:21 /usr/local/src]#

The PS1 variable is used to define the command line prompt, and we can define our favorite prompt according to our own needs. The options supported by PS1 are:

\d: Display the date, the format is "Sunday Month Day"

\H: Display the full hostname. Such as the default hostname: localhost.localdomain

\h: Display the abbreviated hostname. Default is: localhost

\T: Display 12-hour time in the format of HH:MM:SS

\t: Display 24-hour time in the format HH:MM:SS

\A: Display 24-hour time in the format of HH:MM

\@: Display 12-hour time in the format HH:MM am/pm

\u: Display the current user name

\v: Display Bash version information

\W: Display the last directory of the current directory

\w: Display the full path of the current directory

\#: The first command to execute

\$: Prompt. If it is root, it will display the prompt as "#", if it is a normal user, it will display the prompt as "$"

3. LANG language variables 

The LANG variable defines the main language environment of the Linux system. The default value of this variable is:

[root@localhost ~]#echo $LANG

zh_CN.UTF-8

(Because when we installed Linux, we chose to install in Chinese. Therefore, the default subject language variable is "zh_CH.UTF-8") The Linux system supports many languages. We directly count the number of 735. The command is:     

[root@localhost src]#locale -a | wc -l

View all current languages:

[root@localhost ~]#locale

Set language format: LANG=zh_CN.UTF-8

        English: LANG = en_US.UTF-8

4. Positional parameter variables

positional parameter variable

effect

$n

n is a number, $0 represents the command itself, $1-$9 represents the first to ninth parameters, and the parameters above ten should be enclosed in curly brackets Example ${10}

$*

This variable represents all parameters on the command line, and $* treats all parameters as a whole.

$@

This variable represents all parameters on the command line, but $@ treats each parameter separately

$#

This variable represents the number of all arguments on the command line.

[root@localhost ~]# cat canshuan3.sh

#!/bin/bash

for i in "$*" #Define the for loop, there are several values ​​after in, how many times the for will loop. $* is enclosed in double quotes.

do #Each time the loop will assign the value after in to the variable.

echo "$i" #shell treats all parameters in $* as a whole, so this for loop will only loop once.

done #Print the value of $i.

for a in "$@" #In there are several values ​​behind the for loop several times, each time assigning the value to the variable a

do #But the shell treats each parameter in $@ as independent, so if there are several parameters in $@, it will loop several times.

echo "$a" #Output the value of variable a.

done

echo "$#" $# outputs the number of arguments.

5. Predefined variables

predefined variables

effect

$?

The return status of the last command executed. If the value of this variable is 0, it proves that the previous command was executed correctly. If the value of this variable is non-zero, it proves that the previous command was executed incorrectly.

$$

Process ID (PID) of the current process

$!

Process ID (PID) of the last process running in the background

Guess you like

Origin blog.csdn.net/weixin_46659843/article/details/123652111