Linux命令set的某些用法

在Linux下可以"help set"查看set命令的帮助:

[root@localhost ~]# help set
set: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
    Set or unset values of shell options and positional parameters.
    设置或者复位shell选项的值和位置参数的值.

    Change the value of shell attributes and positional parameters, or
    display the names and values of shell variables.
    改变shell属性的值和位置参数的值, 或者显示shell变量的名字和值.

    Options:
    (我删去了一些选项)
      -h  Remember the location of commands as they are looked up.
      -H  Enable ! style history substitution.  This flag is on
          by default when the shell is interactive.
      -v  Print shell input lines as they are read.
      -x  Print commands and their arguments as they are executed.
      --  Assign any remaining arguments to the positional parameters.
          If there are no remaining arguments, the positional parameters
          are unset.
          将任何剩余的参数分配给位置参数, 如果没有剩余的参数, 就会将位置参数复位.
      -   Assign any remaining arguments to the positional parameters.
          The -x and -v options are turned off.
          将任何剩余的参数分配给位置参数. -x和-v选项会被关闭.

    Using + rather than - causes these flags to be turned off.  The
    flags can also be used upon invocation of the shell.  The current
    set of flags may be found in $-.  The remaining n ARGs are positional
    parameters and are assigned, in order, to $1, $2, .. $n.  If no
    ARGs are given, all shell variables are printed.
    使用 + 而不是 - 会导致这些标志被关闭. 这些标志还可以在调用shell时被使用.
    这些标志的当前的设置可以在 $- 中被找到. 剩余的n个参数是位置参数, 它们会被
    按顺序分配给 $1, $2, .. $n. 如果没有给出参数, 所有的shell变量会被打印出来.

    Exit Status:
    Returns success unless an invalid option is given.
    返回success, 除非给出了一个无效的参数.
[root@localhost ~]#
可以看到set命令不接受"--help"和"--version"参数,"-h"不是"--help"的简写,"-v"不是"--version"的简写。
set -- 1a 2b 3c 4d
set  - 1a 2b 3c 4d
set    1a 2b 3c 4d
作用相同, 都是"Assign any remaining arguments to the positional parameters.".
set --
复位"位置参数"。
set -
没有任何作用(可能有"关闭-x和-v选项"的作用)。
下面是一个例子:

[root@localhost ~]# set 1a 2b 3c 4d
[root@localhost ~]# for((i=1;i<=$#;i++)); do eval echo \${${i}}; done
1a
2b
3c
4d
[root@localhost ~]# set 1x 2y 3z
[root@localhost ~]# for((i=1;i<=$#;i++)); do eval echo \${${i}}; done
1x
2y
3z
[root@localhost ~]# set -
[root@localhost ~]# for((i=1;i<=$#;i++)); do eval echo \${${i}}; done
1x
2y
3z
[root@localhost ~]# set --
[root@localhost ~]# for((i=1;i<=$#;i++)); do eval echo \${${i}}; done
[root@localhost ~]#
另外,对于$-等的总结如下(copy的):
$0 这个程式的执行名字。
$n 这个程式的第n个参数值,n=1..9 。
$* 这个程式的所有参数,此选项参数可超过9个。
$# 这个程式的参数个数。
$$ 这个程式的PID(脚本运行的当前进程ID号)。
$! 执行上一个背景指令的PID(后台运行的最后一个进程的进程ID号)。
$? 执行上一个指令的返回值 (显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误)。
$- 显示shell使用的当前选项,与set命令功能相同。
$@ 跟$*类似,但是可以当作数组用。
完。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值