gpt4 book ai didi

bash - 如何使用带空格的参数在 Bash 中调用应用程序

转载 作者:行者123 更新时间:2023-12-04 19:02:51 25 4
gpt4 key购买 nike

我有一个 bash 文件,它传递了包含空格的参数。 bash 文件如下所示:

#!/bin/bash
another_app "$1"

但是,它不是像我认为的那样将参数作为单个参数处理,而是根据空格的数量将其作为多个参数处理。例如,如果我这样调用我的 bash 文件:
my_app "A Number Of Words"

然后“another_app”应用程序获得了 4 个不同的参数,而不是一个。我怎样才能将单个参数传递给第二个应用程序?

最佳答案

其他的都是正确的,这在一定程度上取决于第二个应用程序如何处理参数。您还可以对参数的传递方式进行一些控制。您可以通过引用或使用@steve 提到的“$@”变量来做到这一点

例如 app1.sh

#!/bin/bash 

echo "Argument with no quotes"
./another_app.sh $1

echo "Argument with quotes"
./another_app.sh "$1"

echo "Argument with \$@"
./another_app.sh "$@"

和另一个_app.sh
#!/bin/bash
echo "Inside $0" 
echo "Number of args passed to me: $#"

for X in "${@}"
do
echo $X
done
echo "Exiting $0"

关于bash - 如何使用带空格的参数在 Bash 中调用应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31084111/

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