如何解决以下问题。
<p>
This <span data-number="1">sentence</span> has an underlined <span data-number="2">text</span>.
</p>
<p>
This <span data-number="1">sentence</span> has<br>
an underlined <span data-number="2">text</span>.
</p>
现在执行脚本:
myscript.sh
echo First argument: $1
echo Second argument: $2
for i in $* do
echo $i
done
当我打印$ 1时,我得到的值是“ this is” 但内部循环第一个参数为“ this”
我想在循环执行时打印
$./myscript.sh "this is" value
First argument: this is
Second argument: value
this
is
value
答案 0 :(得分:2)
这是由于使用$*
而不是"$@"
来获取所有参数。来自ShellCheck:
$ *(不加引号)会被分词和修饰。
假设您有三个参数:baz,foo bar和*
“ $ @”将完全扩展为:baz,foo bar和*
$ *将扩展为其他多个参数:baz,foo,bar,file.txt 和otherfile.jpg
对于所需的行为,您可以执行以下操作:
echo "First argument: $1"
echo "Second argument: $2"
for i in "$@"
do echo "$i"
done