小编Nil*_*esh的帖子

bash 变量值中转义空格而不加引号

在 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)

command-line bash

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

bash ×1

command-line ×1