在 bash 脚本中,我们可以使用 转义空格\ 。有什么方法可以从代码中执行相同的操作吗?
$ a=hello\ world
$ echo $a
hello world
$ b=${a:?} # ? used as dummy; it will not work
$ echo $b
hello\ world
Run Code Online (Sandbox Code Playgroud)
来电者.bash
#!/bin/bash
name=$1
if [[ -n $name ]]; then
where_query="--where name $name"
fi
mycommand $where_query
Run Code Online (Sandbox Code Playgroud)
mycommand是外部程序,我们无法修改。添加虚拟代码仅用于说明目的。
#!/bin/bash
for i in "${@}"
do
echo "$i"
done
Run Code Online (Sandbox Code Playgroud)
实际的
$ caller.bash "foo bar"
--where
name
foo
bar
Run Code Online (Sandbox Code Playgroud)
预期的
$ caller.bash "foo bar"
--where
name
foo bar
Run Code Online (Sandbox Code Playgroud)