如何理解`exec python3 "$0" "$@"`之后的"$0" "$@"`

问题描述 投票:0回答:1

我不太遵循https://github.com/nodejs/node/blob/main/configure#L6-L16中的以下代码片段。

特别是,

"$0" "$@"
之后的
sh -c ```<command>``` 
是什么意思?

目前,我知道通过运行

./<this_file_name>
,一旦我们找到现有版本,程序将检查现有的可用Python并执行相同的文件(因为
exec
将在成功完成后退出当前程序)。


#!/bin/sh

# Locate an acceptable Python interpreter and then re-execute the script.
# Note that the mix of single and double quotes is intentional,
# as is the fact that the ] goes on a new line.
_=[ 'exec' '/bin/sh' '-c' '''
command -v python3.11 >/dev/null && exec python3.11 "$0" "$@"
command -v python3.10 >/dev/null && exec python3.10 "$0" "$@"
exec python "$0" "$@"
''' "$0" "$@"
]
del _
import sys
...
python bash sh zsh exec
1个回答
0
投票

来自

man sh

 -c 从command_string操作数读取命令而不是
    来自标准输入。特殊参数0将被设置
    来自 command_name 操作数和位置参数
    ($1、$2 等)从剩余的参数操作数设置。

一个例子:

/bin/sh -c '
   echo \$0 is $0
   echo "All the arguments are : $@"
' dollar-0 arg1 arg2 arg3 ...

输出是:

$0 is dollar-0
All the arguments are : arg1 arg2 arg3 ...
© www.soinside.com 2019 - 2024. All rights reserved.