basht-6ren">
gpt4 book ai didi

bash -c 具有不同的参数语义($1 变为 $0!)

转载 作者:行者123 更新时间:2023-11-29 08:53:15 27 4
gpt4 key购买 nike

$ bash -c 'echo "0 is $0 1 is $1"' abc def
0 is abc 1 is def

$ echo 'echo "0 is $0 1 is $1"' > bashtest

$ bash bashtest abc def
0 is bashtest 1 is abc

第二次运行相当于我用shebang把bash test变成shellscript然后直接运行...

基本上我想知道为什么 abc 不总是 $1。当使用 bash -c 运行时,它将变为 $0

最佳答案

我也不知道这个。但是 man page提到它:

-c string: If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.

ARGUMENTS 部分有更详细的解释:

ARGUMENTS

If arguments remain after option processing, and neither the -c nor the -s option has been supplied, the first argument is assumed to be the name of a file containing shell commands. If bash is invoked in this fashion, $0 is set to the name of the file, and the positional parame‐ ters are set to the remaining arguments. Bash reads and executes com‐ mands from this file, then exits. Bash's exit status is the exit sta‐ tus of the last command executed in the script. If no commands are executed, the exit status is 0. An attempt is first made to open the file in the current directory, and, if no file is found, then the shell searches the directories in PATH for the script.

关于bash -c 具有不同的参数语义($1 变为 $0!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17253384/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com