这个问题在这里已有答案:
用一个例子可以更好地解释这一点。我正在构建一个小的bash脚本,每1秒重新运行一次传递的命令行。这是我的第一次尝试:
- 文件wtbash
watch -n1 "$@"
如果我发出:
wt "df -h | grep sda"
它工作正常。我希望能够做到:
wt df -h | grep sda
而且工作方式一样
也就是说,我想将“df -h | grep sda”视为单个字符串参数
有
一个应用程序
一个变量!来自man bash
:
* Expands to the positional parameters, starting from one. When the expan‐
sion is not within double quotes, each positional parameter expands to a
separate word. In contexts where it is performed, those words are sub‐
ject to further word splitting and pathname expansion. When the expan‐
sion occurs within double quotes, it expands to a single word with the
value of each parameter separated by the first character of the IFS spe‐
cial variable. That is, "$*" is equivalent to "$1c$2c...", where c is
the first character of the value of the IFS variable. If IFS is unset,
the parameters are separated by spaces. If IFS is null, the parameters
are joined without intervening separators.
所以将脚本更改为:
#!/bin/bash
watch -n1 "$*"
但这不适用于管道。你仍然需要逃避:
wt df -h \| grep sda