gpt4 book ai didi

linux - 如何将参数传递给 source 命令调用的脚本?

转载 作者:IT王子 更新时间:2023-10-29 00:14:10 45 4
gpt4 key购买 nike

我正在通过 source 命令调用脚本并想将参数传递给脚本。

我检查了 man source,bash 返回:

: [arguments]
No effect; the command does nothing beyond expanding arguments and performing any specified redirections. A zero exit code is returned.

source filename [arguments]
Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename. If filename does not contain a slash, file names in PATH are used to find the directory containing filename. The file searched for in PATH need not be executable. When bash is not in posix mode, the current directory is searched if no file is found in PATH. If the sourcepath option to the shopt builtin command is turned off, the PATH is not searched. If any arguments are supplied, they become the positional parameters when filename is executed. Otherwise the positional parameters are unchanged. The return status is the status of the last command exited within the script (0 if no commands are executed), and false if filename is not found or cannot be read.

没有例子,看不懂。

最佳答案

创建一个包含以下内容的文件 test.sh:

echo "I was given $# argument(s):"
printf "%s\n" "$@"

然后从交互式 shell session 中获取它:

$ source ./test.sh a 'b c'
I was given 2 argument(s):
a
b c

因此您可以像在常规 bash 脚本中一样访问参数,使用 $@$1$2$3

为了比较,将其作为常规脚本运行:

$ bash ./test.sh a 'b c'
I was given 2 argument(s):
a
b c

关于linux - 如何将参数传递给 source 命令调用的脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39768712/

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