$ ls -l /tmp/test/my\ dir/
total 0
Run Code Online (Sandbox Code Playgroud)
我想知道为什么以下运行上述命令的方法失败或成功?
$ abc='ls -l "/tmp/test/my dir"'
$ $abc
ls: cannot access '"/tmp/test/my': No such file or directory
ls: cannot access 'dir"': No such file or directory
$ "$abc"
bash: ls -l "/tmp/test/my dir": No such file or directory
$ bash -c $abc
'my dir'
$ bash -c "$abc"
total 0
$ eval $abc
total 0
$ eval "$abc"
total 0
Run Code Online (Sandbox Code Playgroud) 我有一个在 Linux 上运行的脚本,它接受一些参数。
我想做类似的事情:
if [[ $CONDITION == "true" ]]; then
script param1 --param2
else
script param1
fi
Run Code Online (Sandbox Code Playgroud)
我想避免if的分叉路径。
有没有更优化的方法来传递第二个参数?
我试图通过在 bash 变量中放置重复参数来清理我的 shell 脚本。
我知道的最好的方法是将它放在一个数组中并传递它......但这似乎不起作用。如何将所有参数存储在变量中?
这不起作用:
TESTP=( "-path" "\"./var/*\"")
echo ${TESTP[@]}
# Results in: -path "./var/*"
find ${TESTP[@]}
# Returns no results.
Run Code Online (Sandbox Code Playgroud)
Whilefind -path "./var/*"确实返回 var 下的所有文件。