"set - $VARIABLE" 在 bash 中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45740724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:22:44  来源:igfitidea点击:

What does "set - $VARIABLE" mean in bash?

bashshell

提问by Give_me_5_dolloars

Recenly, i read Learning the bash Shell, 3rd Editionin chapter 7 section "Reading user input". When i See the code below,

最近,我在第 7 章“阅读用户输入”部分阅读了学习 bash Shell,第 3 版。当我看到下面的代码时,

echo 'Select a directory:'
done=false

while [ $done = false ]; do
        do=true
        num=1
        for direc in $DIR_STACK; do
                echo $num) $direc
                num=$((num+1))
        done
        echo -n 'directory? '
        read REPLY

        if [ $REPLY -lt $num ] && [ $REPLY -gt 0 ]; then
                set - $DIR_STACK

                #statements that manipulate the stack...

                break
        else
                echo 'invalid selection.'
        fi
done

What is the exact meaning of set - $DIR_STACK?

的确切含义是set - $DIR_STACK什么?

回答by Charles Duffy

This will string-split and glob-expand the contents of $DIR_STACK, putting the first in $1, the second in $2, etc. It's not good practice -- well-written scripts don't rely on string splitting (see the advice at the very top of BashPitfalls, and many of the bugs below are caused by failures to heed that advice).

这将字符串拆分和全局扩展 的内容$DIR_STACK,将第一个放入$1,第二个放入$2等。这不是一个好习惯——编写良好的脚本不依赖于字符串拆分(请参阅最顶部的建议BashPitfalls以及下面的许多错误都是由于未能听取该建议而引起的)。

It's more properly written with --, not -. This is defined by POSIX Utility Syntax Guidelines, entry #10:

用 来写更合适--,而不是用-。这是由POSIX 实用程序语法指南,条目 #10 定义的:

The first --argument that is not an option-argument should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the -character.

第一个--不是选项参数的参数应该被接受为指示选项结束的分隔符。任何后面的参数都应该被视为操作数,即使它们以-字符开头。



The use of setto change the argument list ($1, $2, etc) is also specified by POSIX, though (again) the standard specifies --, not -:

采用set改变参数列表($1$2,等)也由POSIX指定的,虽然(再次)的标准规定--,不-

The remaining arguments [ed: after processing options]shall be assigned in order to the positional parameters. The special parameter #shall be set to reflect the number of positional parameters. All positional parameters shall be unset before any new values are assigned.

The special argument --immediately following the set command name can be used to delimit the arguments if the first argument begins with +or -, or to prevent inadvertent listing of all shell variables when there are no arguments. The command set --without argument shall unset all positional parameters and set the special parameter #to zero.

剩余的参数[ed: after processing options]应按顺序分配给位置参数。#应设置特殊参数以反映位置参数的数量。在分配任何新值之前,应取消设置所有位置参数。

--如果第一个参数以+or开头,紧跟在 set 命令名称之后的特殊参数可用于分隔参数-,或者在没有参数时防止无意中列出所有 shell 变量。set --不带参数的命令应取消设置所有位置参数并将特殊参数#设置为零。