特殊变量

在Shell中的特殊变量主要分别两种位置参数变量、状态变量两种。

位置参数变量

Shell中的位置参数变量主要是指0、0、1、$#等,主要用于从命令行、函数或脚本执行等地方传递参数。详细说明如下所示:

  • $0:获取当前执行的Shell脚本的文件名,如脚本中包含路径,则输出也包括路径
  • $n:获取当前执行的Shell脚本的第n个参数值,如n>9,则需要使用用大括号,如${10},各参数间用空格进行分隔
  • $#:获取当前执行的Shell脚本传入参数的总个数
  • $*:获取当前执行的Shell所有传入参数,如不加引号则与$@功能相同,如果添加双引号**"",则表示将所有传入参数视为单个字符串"",则表示将所有传入参数视为单个字符串"1 223"
  • $@:获取当前执行的Shell所有传入参数,**如不加引号则与$*功能相同,如果添加双引号"@",则表示将所有传入参数视为独立的字符串"@",则表示将所有传入参数视为独立的字符串"1" "2""2""3"

当""和""和"@"都添加双引号时,两者的功能有所区别;如不加,则功能相同,无区别。

位置参数变量示例

1、示例一:

[root@localhost Test]# cat para.sh
#!/bin/bash
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15}
echo '$0 is:' $0
echo '$1 is:' $1
echo '$12 is:' ${12}
echo '$# is:' $#
echo '$* is:' $*
echo '"$*"is:' "$*"
echo '$@ is:' $@
echo '"$@"is:' "$@"
# 输出结果
[root@localhost Test]# bash ~/Test/para.sh {a..z}
a b c d e f g h i j k l m n o
$0 is: /root/Test/para.sh
$1 is: a
$12 is: l
$# is: 26
$* is: a b c d e f g h i j k l m n o p q r s t u v w x y z
"$*"is: a b c d e f g h i j k l m n o p q r s t u v w x y z
$@ is: a b c d e f g h i j k l m n o p q r s t u v w x y z
"$@"is: a b c d e f g h i j k l m n o p q r s t u v w x y z

1、传入的参数个数多于脚本定义的参数时,则多出的参数会忽略

2、传入的参数中如使用双引号,则会当作一个参数值进行传递

3、位置参数如大于9,需要使用${}进行传递

2、示例二:

[root@localhost Test]# cat testposition.sh 
#!/bin/bash
echo '$# $1 $2 $3 $* $@'
echo $# $1 $2 $3 $* $@
echo "************"
echo '$*'
for tmp in $*
 do
 echo $tmp
 done
echo "************"
echo "@@@@@@@@@@@@"
echo '$@'
for temp in $@
 do
 echo $temp
 done
echo "@@@@@@@@@@@@"

echo '"*"*"*"*"*"*'

echo '$*'
for i in "$*"
 do
 echo $i
 done
echo '"*"*"*"*"*"*'

echo '"@"@"@"@"@"@'

echo '$@'
for j in "$@"
 do
 echo $j
 done
echo '"@"@"@"@"@"@'

[root@localhost Test]# bash testposition.sh "Hello Jack" Welcome "to Shanghai"
$# $1 $2 $3 $* $@
3 Hello Jack Welcome to Shanghai Hello Jack Welcome to Shanghai Hello Jack Welcome to Shanghai
************
$* # 未加双引号,所以会输出全部参数,则第一个和第三个参数会拆开
Hello
Jack
Welcome
to
Shanghai
************
@@@@@@@@@@@@
$@ # 未加双引号,所以会输出全部参数,则第一个和第三个参数会拆开
Hello
Jack
Welcome
to
Shanghai
@@@@@@@@@@@@
"*"*"*"*"*"*
$* # 添加双引号后,传入的参数全部当一个参数进行输出
Hello Jack Welcome to Shanghai
"*"*"*"*"*"*
"@"@"@"@"@"@
$@ # 添加双引号后,传入的参数全部当独立的参数进行输出
Hello Jack
Welcome
to Shanghai
"@"@"@"@"@"@

状态变量

  • $"htmlcode">
    [root@localhost Test]# ll /etc/profile
    -rw-r--r--. 1 root root 1819 4月 11 2018 /etc/profile
    [root@localhost Test]# echo $"htmlcode">
    
    [root@localhost Test]# cat testPID.sh
    #!/bin/bash
    echo $$ > /tmp/test.pid
    sleep 300
    
    [root@localhost Test]# bash testPID.sh & # 将当前脚本调用到后台执行
    [1] 1671
    [root@localhost Test]# ps -ef | grep testPID | grep -v grep
    root 1671 23706 0 16:37 pts/0 00:00:00 bash testPID.sh # 查询PID

    3、!示例:!示例:!功能类似于$$,只不过是获取上一次执行脚本的PID

    [root@localhost Test]# bash testPID.sh &
    [1] 24078
    [root@localhost Test]# echo $!
    24078 # 打印上一次在后台执行的进程号
    [root@localhost Test]# ps -ef | grep testPID | grep -v grep
    root 24078 23706 0 16:42 pts/0 00:00:00 bash testPID.sh

    4、$_示例:

    [root@localhost Test]# bash para.sh {a..z}
    a b c d e f g h i j k l m n o
    $0 is: para.sh
    $1 is: a
    $12 is: l
    $# is: 26
    $* is: a b c d e f g h i j k l m n o p q r s t u v w x y z
    "$*"is: a b c d e f g h i j k l m n o p q r s t u v w x y z
    $@ is: a b c d e f g h i j k l m n o p q r s t u v w x y z
    "$@"is: a b c d e f g h i j k l m n o p q r s t u v w x y z
    [root@localhost Test]# echo $_
    z # 打印最后一个传入的参数值

    Bash 内置变量

    常用的内部命令有echo、eval、exec、export、read、shift、exit。

    echo

    主要用于打印信息,其命令格式如下所示:

    echo [options] args

    常用参数如下所示:

    参数选项 说明 -n 不换行输出内容 -e 解析转义字符

    echo常用转义字符如下:

    转义字符 说明 \n 换行 \r 回车 \t Tab \b 退格 \v 纵向制表符

    eval

    当Shell程序运行到eval语句时,将读入参数args,并将它们组合成一个新的命令而后执行。其命令格式如下所示:

    eval args

    exec

    exec主要用于在不创建新的子进程的情况下,转而执行指定的命令,当指定命令执行完后,则终止该进程。其命令格式如下所示:

    exec args

    当使用exec打开文件后,read命令每次都会将文件指针移动到下一行进行读取,直至结束。因此常用来处理文件内容。

    read

    从标准输入读取变量或字符串等信息并传递给其他变量,其命令格式如下所示

    read args

    shift

    对传入的位置参数依次向左移动一个位置,并使用位置参数$#减1,直至0为止。其命令格式如下所示:

    shift postition args

    shift如果不带参数,则默认左移1位

    如传入的参数为112 3,如执行一次shift,则之前的3,如执行一次shift,则之前的3变成2,2,2变成1,1,1消失。

    exit

    常用于退出Shell,在日常使用过程中可使用exit num来自定义返回状态数。

    Bash 内置变量示例

    1、echo

    [root@localhost Test]# echo "Test";echo "Dao"
    Test
    Dao
    [root@localhost Test]# echo -n "Test";echo "Dao"
    TestDao
    [root@localhost Test]# echo -e "Test\tName\n Dao"
    Test Name
     Dao

    2、eval

    [root@localhost Test]# cat eval.sh
    #!/bin/bash
    
    echo "No eval"
    echo \$$#
    echo "Add eval"
    eval echo \$$#
    
    [root@localhost Test]# bash eval.sh a b
    No eval
    $2 # 未添加evel时,$#为2,则输出$2
    Add eval
    b # 添加evel后,则重新对传入的参数进行解析,则输出传入的第2个参数

    3、exec

    [root@localhost Test]# exec ls
    eval.sh para.sh ping.sh testPID.sh testposition.sh
    [admin@localhost ~]$ # 在执行exec后则终止当前Shell进程,因此从root用户退出到普通用户
    # 与read一起读取文件
    [root@localhost ~]# seq 5 > /tmp/rand.log
    [root@localhost ~]# cat /tmp/rand.log 
    1
    2
    3
    4
    5
    [root@localhost Test]# cat exec.sh
    #!/bin/bash
    exec < /tmp/rand.log
    while read line
     do
     echo $line
     done
    echo "Completed"
    
    [root@localhost Test]# bash exec.sh
    1
    2
    3
    4
    5
    Completed

    4、read

    可以参考公众号中read命令一文

    5、shift

    [root@localhost Test]# cat shift.sh
    #!/bin/bash
    echo $1 $2 $3 $4 $5
    until [ -z $1 ]
    do
     echo $@
     shift 1
    done
    [root@localhost Test]# bash shift.sh {1..5}
    1 2 3 4 5
    1 2 3 4 5
    2 3 4 5
    3 4 5
    4 5
    5

    变量扩展

    变量扩展说明

    Shell中变量扩展说明如下所示:

    • ${var}:返回${var}的内容
    • ${#var}:返回${var}的字符长度
    • ${var:offset}:返回${var}从位置offset之后开始提取字符至结束
    • ${var:offset:length}:返回${var}从offset之后,提取长度为length的字符
    • ${var#word}:返回从${var}开头开始删除最短匹配的word子符串
    • ${var##word}:返回从${var}开头开始删除最长匹配的word子符串
    • ${var%word}:返回从${var}结尾开始删除最短匹配的word子符串
    • ${var%%word}:返回从${var}结尾开始删除最长匹配的word子符串
    • ${var/oldstring/newstring}:使用newstring替换第一个匹配的字符oldstring
    • ${var//oldstring/newstring}:使用newstring替换所有匹配的字符oldstring
    • ${var:-word}:如果变量var的值为空或未赋值,则将word做为返回值,常用于防止变量为空或未定义而导致的异常
    • ${var:=word}:如果变量var的值为空或未赋值,则将word赋值给var并返回其值。
    • ${var:"htmlcode">
      [root@localhost init.d]# var="This is test string"
      [root@localhost init.d]# echo $var
      This is test string
      [root@localhost init.d]# echo ${var}
      This is test string
      [root@localhost init.d]# echo ${#var} # 统计字符长度
      19
      [root@localhost init.d]# echo ${var:5} # 从第5个位置开始截取字符
      is test string
      [root@localhost init.d]# echo ${var:5:2} # 从第5个位置开始截取2个字符
      is
      [root@localhost init.d]# echo ${var#This} # 从开头删除最短匹配的字符 is
      is test string
      [root@localhost init.d]# echo ${var##This} # 从开头删除最长匹配的字符 is
      is test string
      [root@localhost init.d]# echo ${var%g} # 从结尾删除最短匹配的字符 is
      This is test strin
      [root@localhost init.d]# echo ${var%%g} # 从结尾删除最长匹配的字符 is
      This is test strin
      [root@localhost init.d]# echo ${var/is/newis} # 替换第一个匹配的字符
      Thnewis is test string
      [root@localhost init.d]# echo ${var//is/newis} # 替换所有匹配到的字符
      Thnewis newis test string
      
      [root@localhost init.d]# echo $centos # 变量未定义
      
      [root@localhost init.d]# echo ${centos:-UNDEFINE} # 变量为空,返回UNDEFINE
      UNDEFINE
      [root@localhost init.d]# centos="CentOS"
      [root@localhost init.d]# echo ${centos:-UNDEFINE} # 变量已经定义,返回变量本身的值
      CentOS
      [root@localhost init.d]# unset centos # 取消变量值
      [root@localhost init.d]# echo $centos
      
      [root@localhost init.d]# result=${centos:=UNDEFINE}
      [root@localhost init.d]# echo $result
      UNDEFINE
      [root@localhost init.d]# echo $centos # 变量值为空,则将UNDEFINE赋值给centos
      UNDEFINE
      [root@localhost init.d]# unset centos
      [root@localhost init.d]# echo ${centos:"IS DEFINED"
      [root@localhost init.d]# echo ${centos:"do"
      [root@localhost init.d]# echo ${centos:+do nothing} # 变量已赋值,则输出自定义的消息
      do nothing

      总结

      以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

标签:
shell特殊变量,shell变量,shell定义变量

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
内蒙古资源网 Copyright www.nmgbbs.com

评论“Shell编程之特殊变量和扩展变量详解”

暂无“Shell编程之特殊变量和扩展变量详解”评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。