通过 shell 变量传递包含空格的参数
Pass parameters that contain whitespaces via shell variable
我有一个程序,我想通过从 shell 变量传递参数来调用它。在整个问题中,我将假设它是由
给出的
#!/bin/sh
echo $#
即它打印出传递给它的参数的数量。我们称它为 count-args
.
我这样调用我的程序:
X="arg1 arg2"
count-args $X
这个效果很好。但是现在我的一个论点中有一个空格,我找不到逃避它的方法,例如以下内容不起作用:
X="Hello\ World"
X="Hello\ World"
X="'Hello World'"
在所有情况下,我的程序 count-args
打印出 2
。我想找到一种方法,以便我可以传递字符串 Hello World
并将其 returns 1
改为传递。怎么样?
澄清一下:我不想将所有参数作为单个字符串传递,例如
X="Hello World"
count-args $X
应该打印出 2
。我想要一种传递包含空格的参数的方法。
count-args "$X"
bash 中的引号确保变量 X 的全部内容作为单个参数传递。
您的计数脚本:
$ cat ./params.sh
#!/bin/sh
echo $#
为了完整起见,这里是各种参数发生的情况:
$ ./params.sh
0
$ ./params.sh 1 2
2
$ ./params.sh
0
$ ./params.sh 1
1
$ ./params.sh 1 2
2
$ ./params.sh "1 2"
1
这里是你得到的变量:
$ XYZ="1 2" sh -c './params.sh $XYZ'
2
$ XYZ="1 2" sh -c './params.sh "$XYZ"'
1
更进一步:
$ cat params-printer.sh
#!/bin/sh
echo "Count: $#"
echo "1 : ''"
echo "2 : ''"
我们得到:
$ XYZ="1 2" sh -c './params-printer.sh "$XYZ"'
Count: 1
1 : '1 2'
2 : ''
这看起来像你想要做的。
现在:如果你有一个你无法控制的脚本,你也无法控制调用脚本的方式。然后,您几乎无法阻止带有空格的变量变成多个参数。
在 Whosebug 上有很多关于此的问题表明您需要能够控制命令的调用方式,否则您无能为力。
Passing arguments with spaces between (bash) script
Passing a string with spaces as a function argument in bash
Passing arguments to a command in Bash script with spaces
哇!这个之前被问过很多次了:
这可以用xargs
解决。通过替换
count-args $X
和
echo $X | xargs count-args
我可以使用反斜杠来转义 $X
中的空格,例如
X="Hello\ World"
echo $X | xargs count-args
打印出 1 并且
X="Hello World"
echo $X | xargs count-args
打印出 2。
使用数组存储多个包含 space 的参数。
$ args=("first one" "second one")
$ count-args "${args[@]}"
2
我有一个程序,我想通过从 shell 变量传递参数来调用它。在整个问题中,我将假设它是由
给出的#!/bin/sh
echo $#
即它打印出传递给它的参数的数量。我们称它为 count-args
.
我这样调用我的程序:
X="arg1 arg2"
count-args $X
这个效果很好。但是现在我的一个论点中有一个空格,我找不到逃避它的方法,例如以下内容不起作用:
X="Hello\ World"
X="Hello\ World"
X="'Hello World'"
在所有情况下,我的程序 count-args
打印出 2
。我想找到一种方法,以便我可以传递字符串 Hello World
并将其 returns 1
改为传递。怎么样?
澄清一下:我不想将所有参数作为单个字符串传递,例如
X="Hello World"
count-args $X
应该打印出 2
。我想要一种传递包含空格的参数的方法。
count-args "$X"
bash 中的引号确保变量 X 的全部内容作为单个参数传递。
您的计数脚本:
$ cat ./params.sh
#!/bin/sh
echo $#
为了完整起见,这里是各种参数发生的情况:
$ ./params.sh
0
$ ./params.sh 1 2
2
$ ./params.sh
0
$ ./params.sh 1
1
$ ./params.sh 1 2
2
$ ./params.sh "1 2"
1
这里是你得到的变量:
$ XYZ="1 2" sh -c './params.sh $XYZ'
2
$ XYZ="1 2" sh -c './params.sh "$XYZ"'
1
更进一步:
$ cat params-printer.sh
#!/bin/sh
echo "Count: $#"
echo "1 : ''"
echo "2 : ''"
我们得到:
$ XYZ="1 2" sh -c './params-printer.sh "$XYZ"'
Count: 1
1 : '1 2'
2 : ''
这看起来像你想要做的。
现在:如果你有一个你无法控制的脚本,你也无法控制调用脚本的方式。然后,您几乎无法阻止带有空格的变量变成多个参数。
在 Whosebug 上有很多关于此的问题表明您需要能够控制命令的调用方式,否则您无能为力。
Passing arguments with spaces between (bash) script
Passing a string with spaces as a function argument in bash
Passing arguments to a command in Bash script with spaces
哇!这个之前被问过很多次了:
这可以用xargs
解决。通过替换
count-args $X
和
echo $X | xargs count-args
我可以使用反斜杠来转义 $X
中的空格,例如
X="Hello\ World"
echo $X | xargs count-args
打印出 1 并且
X="Hello World"
echo $X | xargs count-args
打印出 2。
使用数组存储多个包含 space 的参数。
$ args=("first one" "second one")
$ count-args "${args[@]}"
2