参考链接:https://bash.cyberciti.biz/guide/Main_Page
1、变量可以用来储存数据,或者配置操作。有两种类型的变量:系统变量、用户自定义变量
系统变量由bash shell建立维护,输入下面指令查看所有的系统变量
set
或者
env
或者
printenv
2、下面的这些变量由shell设置,也就是是系统变量
System Variable | Meaning | To View Variable Value Type |
---|---|---|
BASH_VERSION | Holds the version of this instance of bash. | echo $BASH_VERSION |
HOSTNAME | The name of the your computer. | echo $HOSTNAME |
CDPATH | The search path for the cd command. | echo $CDPATH |
HISTFILE | The name of the file in which command history is saved. | echo $HISTFILE |
HISTFILESIZE | The maximum number of lines contained in the history file. | echo $HISTFILESIZE |
HISTSIZE | The number of commands to remember in the command history. The default value is 500. | echo $HISTSIZE |
HOME | The home directory of the current user. | echo $HOME |
IFS | The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is <space><tab><newline>. | echo $IFS |
LANG | Used to determine the locale category for any category not specifically selected with a variable starting with LC_. | echo $LANG |
PATH | The search path for commands. It is a colon-separated list of directories in which the shell looks for commands. | echo $PATH |
PS1 | Your prompt settings. | echo $PS1 |
TMOUT | The default timeout for the read builtin command. Also in an interactive shell, the value is interpreted as the number of seconds to wait for input after issuing the command. If not input provided it will logout user. | echo $TMOUT |
TERM | Your login terminal type. | echo $TERM export TERM=vt100 |
SHELL | Set path to login shell. | echo $SHELL |
DISPLAY | Set X display name | echo $DISPLAY export DISPLAY=:0.1 |
EDITOR | Set name of default text editor. | export EDITOR=/usr/bin/vim |
3、如何获取一个变量的值,并打印
echo "$PATH"
4、printf命令和echo命令差不多
printf "$VARIABLE_NAME\n"
printf "String %s" $VARIABLE_NAME
printf "Signed Decimal Number %d" $VARIABLE_NAME
printf "Floating Point Number %f" $VARIABLE_NAME
5、创建一个变量并赋值,在脚本中非常常见,采用下面的指令
varName=someValue
然后通过echo或者printf打印出来变量的值
echo "$varName"
echo "${varName}"
pritnf "${varName}"
printf "%s\n"${varName}
去变量的值的时候,尽量带上{},这样可以尽可能地避免二义性。
看到default shell variables value这块了。
https://bash.cyberciti.biz/guide/Default_shell_variables_value