All variables in bash are stored as strings, and the ENV command can view the environment variables associated with this terminal process.
View environment variables for a process
1. Get the PID of the program, such as Java
# pgrep Java
23492
2. View the corresponding environment variables
# Cat/proc/23492/environ
3. Format the output so that the view will be clearer than
# Cat/proc/23492/environ |tr ' \ n '
"^@" will be transformed into a newline character.
Assigning values to variables
Var=varlue, if value does not contain white space characters, you do not need to add single or double quotation marks
var = value is an equal operation, note the distinction that many people this place is confusing
Output variables
echo $var or Echo ${var}
For example:
var="${var} (s)"Apple (s)
Get the character length of a variable
echo ${#var}
Special variables
$PS 1 system default: ps1= "[\[email protected]\h \w]\$"
$UID for root user 0 o'clock
$TMOUT Terminal Timeout time
$0,$*, [email protected], $#
[Email protected] sbin]#Cat/usr/local/sbin/test.SH #!/bin/BashEcho '$0='$0 #脚本的名称Echo '$1='$1 #运行脚本的第n个参数Echo '$#='$# #运行脚本参数的个数Echo '$*='$* #所有参数变成一个字符Echo '[Email protected]='[email protected] #所有参数basename$0 #获取文件名dirname$0 #获取文件的目录[email protected] sbin]#SH/usr/local/sbin/test.SHAA bb$0=/usr/local/sbin/test.SH$1=aa$#=2$*=AA Bb[email protected]=AA bbtest.SH/usr/local/sbin
Process state variables
$? Gets the return value that executes the last command (0 is successful, others are failed)
$$ getting the PID of the current shell
$_ gets the last parameter of the previous command or script
!$ executing the previous command
Shell Scripting Learning Summary--variables and environment variables