1.常用系统变量 $HOME、$PWD、$SHELL、$USER
查看系统变量的值
- [root@hadoop scripts]# echo $HOME
- /root
查看全局环境变量 命令env
- [root@hadoop scripts]# env
- SPARK_HOME=/usr/soft/spark
- HOSTNAME=hadoop
- SELINUX_ROLE_REQUESTED=
- TERM=xterm
- SHELL=/bin/bash
- HADOOP_HOME=/usr/soft/hadoop
- HISTSIZE=1000
查看系统全局变量 命令printenv
[root@hadoop scripts]# printenv
查看局部变量用printenv命令 直接打名称
- [root@hadoop scripts]# printenv USER
- root
命令env和printenv只是打印当前环境变量信息的命令,
显示当前 Shell 中所有变量:set
[root@hadoop scripts]# set
基本语法
(1)定义变量:变量名=变量值,注意,=号前后不能有空格
(2)撤销变量:unset 变量名
(3)声明静态变量:readonly 变量,注意:不能 unset
变量定义规则
(1)变量名称可以由字母、数字和下划线组成,但是不能以数字开头,环境变量名建 议大写。 (2)等号两侧不能有空格
(3)在 bash 中,变量默认类型都是字符串类型,无法直接进行数值运算。
(4)变量的值如果有空格,需要使用双引号或单引号括起来。
- [root@hadoop scripts]# a=2
- [root@hadoop scripts]# echo $a
- 2
- [root@hadoop scripts]# echo $my_var
-
- [root@hadoop scripts]# my_var=hello
- [root@hadoop scripts]# echo $my_var
- hello
- [root@hadoop scripts]# my_var=Hello
- [root@hadoop scripts]# echo $my_var
- Hello
以下错误的原因是,等号前后空格,系统会识别成命令
- [root@hadoop scripts]# my_var = Hello
- -bash: my_var: command not found
- [root@hadoop scripts]# my_var="hello, world"
- [root@hadoop scripts]# echo $my_var
- hello, world
双引号和单引号都可以
- [root@hadoop scripts]# my_var='hello, world'
- [root@hadoop scripts]# echo $my_var
- hello, world
用以下发生判断局部变量,判断结果是局部变量
- [root@hadoop scripts]# set | grep my_var
- my_var='hello, world'
- [root@hadoop scripts]# bash
- [root@hadoop scripts]# ps -f
- UID PID PPID C STIME TTY TIME CMD
- root 20191 20187 0 16:53 pts/0 00:00:00 -bash
- root 20504 20191 0 18:18 pts/0 00:00:00 bash
- root 20513 20504 0 18:18 pts/0 00:00:00 ps -f
- [root@hadoop scripts]# echo $my_var
-
- [root@hadoop scripts]# exit
- exit
提升成全局变量
- [root@hadoop scripts]# export my_var #做导出
- [root@hadoop scripts]# echo $my_var
- hello, world
- [root@hadoop scripts]# ps -f #查看父子继承关系
- UID PID PPID C STIME TTY TIME CMD
- root 20191 20187 0 16:53 pts/0 00:00:00 -bash
- root 20525 20191 0 18:22 pts/0 00:00:00 ps -f
- [root@hadoop scripts]# bash #进入子shell
- [root@hadoop scripts]# ps -f #查看父子继承关系
- UID PID PPID C STIME TTY TIME CMD
- root 20191 20187 0 16:53 pts/0 00:00:00 -bash
- root 20526 20191 0 18:22 pts/0 00:00:00 bash
- root 20537 20526 0 18:23 pts/0 00:00:00 ps -f
- [root@hadoop scripts]# echo $my_var #在子shell中查看
- hello, world
在全局变量下的子进程中,进行修改,不影响父shell的结果
- [root@hadoop scripts]# my_var="hello, Linux"
- [root@hadoop scripts]# echo $my_var
- hello, Linux
- [root@hadoop scripts]# exit
- exit
- [root@hadoop scripts]# ps -f
- UID PID PPID C STIME TTY TIME CMD
- root 20191 20187 0 16:53 pts/0 00:00:00 -bash
- root 20548 20191 0 18:28 pts/0 00:00:00 ps -f
- [root@hadoop scripts]# echo $my_var
- hello, world
在全局变量下的子进程中,在修改全局,依然还是hello, world
- [root@hadoop scripts]# bash
- [root@hadoop scripts]# ps -f
- UID PID PPID C STIME TTY TIME CMD
- root 20191 20187 0 16:53 pts/0 00:00:00 -bash
- root 20560 20191 0 18:31 pts/0 00:00:00 bash
- root 20569 20560 0 18:32 pts/0 00:00:00 ps -f
- [root@hadoop scripts]# my_var="hello, Linux"
- [root@hadoop scripts]# export my_var
- [root@hadoop scripts]# echo $my_var
- hello, Linux
- [root@hadoop scripts]# exit
- exit
- [root@hadoop scripts]# echo $my_var
- hello, world
基于脚本的方式实现局部变量提升成全局
- [root@hadoop scripts]# echo $new_var
-
- [root@hadoop scripts]# new_var="hello, linux"
- [root@hadoop scripts]# echo $new_var
- hello, linux
- [root@hadoop scripts]# vim hello2.sh
- [root@hadoop scripts]# cat hello2.sh
- #!/bin/bash
- echo "hello, world"
- echo $my_var
- echo $new_var
- [root@hadoop scripts]# source hello2.sh
- hello, world
- hello, world
- hello, linux
- [root@hadoop scripts]# sh hello2.sh
- hello, world
- hello, world
-
- [root@hadoop scripts]# export new_var
- [root@hadoop scripts]# sh hello2.sh
- hello, world
- hello, world
- hello, linux
默认定义的类型是字符串,数值计算运算符的表达了
- [root@hadoop scripts]# echo $a
- 2
- [root@hadoop scripts]# a=1+5
- [root@hadoop scripts]# echo $a
- 1+5
数值运算
- [root@hadoop scripts]# a=$((1+5))
- [root@hadoop scripts]# echo $a
- 6
- [root@hadoop scripts]# a=$[5+9]
- [root@hadoop scripts]# echo $a
- 14
只读变量readonly
- [root@hadoop scripts]# readonly b=5
- [root@hadoop scripts]# b=10
- -bash: b: readonly variable
- [root@hadoop scripts]# echo $b
- 5
撤销命令unset
- [root@hadoop scripts]# unset a
- [root@hadoop scripts]# unset b #相当于常量,不能撤销
- -bash: unset: b: cannot unset: readonly variable
$n (功能描述:n 为数字,$0 代表该脚本名称,$1-$9 代表第一到第九个参数,十以 上的参数,十以上的参数需要用大括号包含,如${10})
- [root@hadoop scripts]# vim hello2.sh
- #!/bin/bash
- echo "hello, world"
- echo "hello, $1"
- [root@hadoop scripts]# . hello2.sh
- hello, world
- hello,
- [root@hadoop scripts]# . hello2.sh xiaoming
- hello, world
- hello, xiaoming
- [root@hadoop scripts]# vim parameter.sh
- #!/bin/bash
- echo '===============$n================'
- echo scripts name: $0
- echo 1st parament: $1
- echo 2nd parament: $2
- [root@hadoop scripts]# chmod +x parameter.sh
- [root@hadoop scripts]# . parameter.sh abc def
- ===============$n================
- scripts name: -bash
- 1st parament: abc
- 2nd parament: def
- [root@hadoop scripts]# ./parameter.sh abc def
- ===============$n================
- scripts name: ./parameter.sh
- 1st parament: abc
- 2nd parament: def
$# (功能描述:获取所有输入参数个数,常用于循环,判断参数的个数是否正确以及 加强脚本的健壮性)。
参数个数
- [root@hadoop scripts]# vim parameter.sh
- #!/bin/bash
- echo '===============$n================'
- echo scripts name: $0
- echo 1st parament: $1
- echo 2nd parament: $2
- echo '===============$#================'
- echo parameter numbers: $#
- [root@hadoop scripts]# ./parameter.sh
- ===============$n================
- scripts name: ./parameter.sh
- 1st parament:
- 2nd parament:
- ===============$#================
- parameter numbers: 0
- [root@hadoop scripts]# ./parameter.sh abc def
- ===============$n================
- scripts name: ./parameter.sh
- 1st parament: abc
- 2nd parament: def
- ===============$#================
- parameter numbers: 2
$* (功能描述:这个变量代表命令行中所有的参数,$*把所有的参数看成一个整体)
$@ (功能描述:这个变量也代表命令行中所有的参数,不过$@把每个参数区分对待)
- [root@hadoop scripts]# vim parameter.sh
- #!/bin/bash
- echo '===============$n================'
- echo scripts name: $0
- echo 1st parament: $1
- echo 2nd parament: $2
- echo '===============$#================'
- echo parameter numbers: $#
- echo '===============$*================'
- echo $*
- echo '===============$@================'
- echo $@
- [root@hadoop scripts]# ./parameter.sh abc def
- ===============$n================
- scripts name: ./parameter.sh
- 1st parament: abc
- 2nd parament: def
- ===============$#================
- parameter numbers: 2
- ===============$*================
- abc def
- ===============$@================
- abc def
$? (功能描述:最后一次执行的命令的返回状态。如果这个变量的值为 0,证明上一 个命令正确执行;如果这个变量的值为非 0(具体是哪个数,由命令自己来决定),则证明 上一个命令执行不正确了。)127表示未找到命令,1表示一般错误
- [root@hadoop scripts]# echo $?
- 0
- [root@hadoop scripts]# parsmeter.sh
- -bash: parsmeter.sh: command not found
- [root@hadoop scripts]# echo $? #表示未找到命令
- 127