[Shell] Common shell variable usage, $n, $#, $*, $@, $?, $HOME, $PWD, $SHELL, $USER

Variables in Shell

1. System variables

$HOME

Get the current user's working directory

[root@jiangnan ~]# echo $HOME
/root
[root@jiangnan ~]# 

$PWD

Get the current directory

[root@jiangnan tomcat]# echo $PWD
/root/tomcat
[root@jiangnan tomcat]# 

Difference: We can also use pwd to return the current directory, but pwd is a command and PWD is a system variable.

$SHELL

Get the currently used shell parser

[root@jiangnan tomcat]# echo $SHELL
/bin/bash
[root@jiangnan tomcat]# 

$USER

get current user

[root@jiangnan tomcat]# echo $USER
root
[root@jiangnan tomcat]# 

set

Get all variables in shell

[root@jiangnan tomcat]# set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
......
_=clear
colors=/root/.dircolors
[root@jiangnan tomcat]# 

env

Get all environment variables

[root@jiangnan ~]# env
XDG_SESSION_ID=13289
HOSTNAME=jiangnan
TERM=xterm
......
_=/usr/bin/env
OLDPWD=/root/tomcat
[root@jiangnan ~]# 

2. Custom variables

2.1 Basic syntax

define variable: variable = value

Undo variable: unset variable

Declare static variables: readonly variables.

Note: Static variables cannot be unset

2.2 Variable Definition Rules

  • The variable name can consist of letters, numbers and underscores, but cannot start with a number. It is recommended to capitalize the environment variable name.
  • No spaces on either side of the equal sign
  • In bash, the default type of variables is string type, and numerical operations cannot be performed directly.
  • If the value of the variable has spaces, it needs to be enclosed in double or single quotation marks.
  • Use export to promote variables to environment variables for global use

2.3 Examples

# 定义变量A
[root@jiangnan ~]# A=5
[root@jiangnan ~]# echo $A
5
# 给变量A重新赋值
[root@jiangnan ~]# A=6
[root@jiangnan ~]# echo $A
6
# 撤销变量A
[root@jiangnan ~]# unset A
[root@jiangnan ~]# echo $A  # 变量A被撤销了,所以不能打印任何数据

[root@jiangnan ~]# 
# 定义静态变量B
[root@jiangnan ~]# readonly B=6
[root@jiangnan ~]# echo $B
6
# 不能对静态变量撤销
[root@jiangnan ~]# unset B
-bash: unset: B: cannot unset: readonly variable
[root@jiangnan ~]# 
# 变量默认类型都是字符串类型,不能直接运算
[root@jiangnan ~]# C=1+2
[root@jiangnan ~]# echo $C
1+2
[root@jiangnan ~]# D=1
[root@jiangnan ~]# E=2
[root@jiangnan ~]# C=D+E
[root@jiangnan ~]# C=$D+$E
[root@jiangnan ~]# echo $C
1+2
[root@jiangnan ~]# 
# 变量的值如果有空格,需要使用双引号或单引号括起来
[root@jiangnan ~]# H=I love China
-bash: love: command not found
[root@jiangnan ~]# H='I love China'
[root@jiangnan ~]# echo $H
I love China
[root@jiangnan ~]# 
# 提升H为环境变量,可以在脚本中使用了
[root@jiangnan tomcat]# H='I love China'
[root@jiangnan tomcat]# export H
[root@jiangnan tomcat]# vi hello.sh
[root@jiangnan tomcat]# cat hello.sh 
#!/bin/bash 
echo $H
[root@jiangnan tomcat]# sh hello.sh 
I love China
[root@jiangnan tomcat]# 

3. Special variables

$n

n is a number, 0 represents the script name, and 1-9 represent the first to ninth parameters. More than ten parameters need to be enclosed in curly brackets, such as ${10}

[root@jiangnan tomcat]# vi variable1.sh 
[root@jiangnan tomcat]# cat variable1.sh 
#!/bin/bash
echo "脚本名称为:"$0
echo "第1个参数为:"$1
echo "第10个参数为:"${10}
[root@jiangnan tomcat]# sh variable1.sh a b c d e f g h i g k l m n  # 参数之间空格隔开
脚本名称为:variable1.sh
第1个参数为:a
第10个参数为:g
[root@jiangnan tomcat]# 

$#

Get the number of all input parameters

[root@jiangnan tomcat]# cat variable2.sh 
#!/bin/bash
echo "参数个数为:"$#
[root@jiangnan tomcat]# sh variable2.sh a b c
参数个数为:3
[root@jiangnan tomcat]# 

This is usually used for loops.

$ and $@

All represent all parameters, but $* treats all parameters as a whole, $@ treats each parameter separately

[root@jiangnan tomcat]# vi variable3.sh
[root@jiangnan tomcat]# cat variable3.sh 
#!/bin/bash
echo $*
echo $@
[root@jiangnan tomcat]# sh variable3.sh a b c
a b c   # 由$*获取
a b c   # 由$@获取
[root@jiangnan tomcat]#  

From here, it can be seen that all parameters are obtained, there is no difference, but the difference can be seen in the for loop.

[root@jiangnan tomcat]# vi variable4.sh 
[root@jiangnan tomcat]# cat variable4.sh 
#!/bin/bash
for parameter1 in "$*"
do
	echo "I love "$parameter1
done
for parameter2 in "$@"
do
	echo "I love "$parameter2
done
[root@jiangnan tomcat]# sh variable4.sh beijing shanghai tianjin
I love beijing shanghai tianjin  # $*把参数当成一个整体
I love beijing                   # $@把每个参数区别对待
I love shanghai
I love tianjin
[root@jiangnan tomcat]# 

$* and $@ use double quotes to become the whole, see the difference.

$?

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.

[root@jiangnan tomcat]# sh variable4.sh a b c
I love a b c
I love a
I love b
I love c
[root@jiangnan tomcat]# echo $?
0  # 返回0证明我的上一条命令sh variable4.sh a b c 执行成功了
[root@jiangnan tomcat]# 

The WeChat public account has been opened first. You can find me by searching for "Jiang Xiaonan and his friends". Friends, you can pay attention. The article will be updated synchronously for easy viewing.

Guess you like

Origin blog.csdn.net/weixin_45842494/article/details/123490655