Article directory
user-defined variable
Variable setting rules
- Variable names can consist of letters, numbers, and underscores, but cannot start with a number.
- In Bash, the default types of variables are strings. If you want to perform numerical operations, you must modify the specified variable type to be numerical.
- Variables use an equal sign to connect values, and there must be no spaces on the left and right sides of the equal sign.
- If the value of the variable has spaces, it needs to be enclosed in single or double quotation marks.
- In the value of a variable, the "\" escape character can be used.
- If the value of the variable needs to be increased, the superposition of the variable value can be performed. However, variables need to be enclosed in double quotes "$variable name" or included with ${variable name}, which is more abstract here, and there will be examples in the following local variables.
- If you assign the result of the command to a variable as a variable value, you need to use backticks or $() to include the command. For example, if we assign the output of the command date to a variable, you need to write:
name=$(date)
- It is recommended to capitalize the names of environment variables for easy distinction.
Variable classification
- User-defined variables.
- Environment variable: This variable mainly saves data related to the operating environment of the system.
- Positional parameter variable: This variable is mainly used to pass parameters or data to the script. The variable name cannot be customized, and the function of the variable is fixed.
- Predefined variable: It is a variable that has been defined in Bash. The variable name cannot be defined, and the function of the variable is fixed.
local variable
Variable definitions:
a=12
Variable overlay:
a=12
b=${a}34
Variable call:
echo $b
Variable view:
set
Variable deletion (e.g. delete variable a):
unset a
environment variable
User-defined variables only take effect in the current shell, while environment variables take effect in the current shell and all subshells of this shell. If the environment variable is written into the corresponding configuration file, the environment variable will take effect in all shells.
For example, if we enter bash on the command line , we will enter the sub-shell. We can use the command pstree to view the process tree, and we can see the shell we are currently in.
If we use the command exit to return to the parent shell and declare the environment variable in the parent shell, the environment variable will also take effect in all its child shells at this time.
Set environment variables
declare variable:
export 变量名=变量值
query variable:
env
Remove variables:
unset 变量名
Environment variables can also be called directly with echo, just like calling methods with local variables.
System common environment variables
PATH
The path PATH where the system looks for commands, separated by colons.
This also explains why we do not need to use absolute paths when executing commands. If the command we entered cannot be found in these paths, then the command will report an error. If we copy the shell script we wrote to these paths You can also execute it without entering an absolute path or a relative path, but if you want to do this, the following method is recommended.
Add the path to PATH :
For example I add the folder I use to install shell files to the PATH.
PATH=$(PATH):/root/sh
PS1
PS1 is the variable that defines the system prompt.
\d:显示日期,格式为“星期 月 日”。
\h:显示简写主机名。如默认主机名“localhost”。
\t:显示24小时制时间,格式为“HH:MM:SS”。
\T:显示12小时制时间,格式为“HH:MM:SS”。
\A:显示24小时制时间,格式为“HH:MM”。
\u:显示当前用户名。
\W:显示当前所在目录的完整名称。
\w:显示当前所在目录的完整路径。
\#:执行的第几个命令。
\$:提示符,如果是root用户会显示提示符为“#”,如果是普通用户会显示提示符为“$”。
Let's first take a look at the default values of the current PS1:
Then we can modify it. For example, I changed **\h , which is the output user name, to output time \t**.
By comparison, it is found that the location of the prompt user name has indeed become the time. If you want to display other things, you can define the prompt yourself.
positional parameter variable
positional parameter variable | effect |
---|---|
$n | n is a number, $0 represents the command itself, $1-$9 represents the ninth parameter of the first channel, and more than ten parameters need to be enclosed in curly brackets, such as ${10}. |
$* | This command represents all parameters on the command line, and $* treats all parameters as a whole. |
$@ | This variable also represents all parameters on the command line, but $@ treats each parameter separately. |
$# | This variable represents the number of all arguments on the command line. |
Explain the parameter variables above:
- $n
For example, I create a shell script and enter the following:
Then execute the script:
chmod 755 canshu1.sh
./canshu1.sh
We found that there is no parameter output, this is because we did not write parameters, if we pass in the corresponding parameters before executing the script file, for example, I pass 12, 13, 14 as parameters to $1, $2, $3, and then The execution will have the corresponding parameter output:
./canshu1.sh 12 13 14
will output 12 13 14 accordingly
- $*,$@,$#
Create a shell script and enter the following:
Feel free to enter some parameters and execute the script:
chmod 755 canshu2.sh
./canshu2.sh 11 222 333 4 555 66 7
Note: Although $* and $@ have the same output, $* treats all parameters as a whole, which is equivalent to one parameter, and the parameters represented by $@ are independent.
predefined variables
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 returned by this variable is not 0 (which number is determined by the command itself), 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 |
Explain the variables above:
- $?
For example, after executing the command ls, if I enter echo $? and output 0, it means that ls is executed correctly.
- $$,$!
Create a shell script and enter the following, & means to put the script in the background:
Execute the script:
chmod 755 canshu3.sh
./canshu3.sh
receive keyboard input
read [选项][变量名]
选项:
-p "提示信息":在等待read输入时,输入提示信息。
-t 秒数:read命令会一直等待输入,使用此选项可以指定等待时间。
-n 字符数:read命令智慧接收指定的字符数,就会执行。
-s 隐藏输入的数据,使用与机密信息的输入。
For example, create a script and write the following:
#!/bin/bash
read -t 30 -p "Please input your name:" name
#提示输入名字并等待30s,把用户的输入保存在变量name中
echo "Name is $name"
read -s -t 30 -p "Please enter your age:" age
#年龄是隐私,用“-s”隐藏输入
echo -e "\n"
read -n 1 -t 30 -p "Please select your gender[M/F]:" gender
#使用“-n 1”选项只接收一个输入字符就会执行(都不用输入回车)
echo -e "\n"
echo "Sex is $gender"