我想实现一个bash函数,该函数将命令的参数作为命令运行,同时(可以选择)在打印命令之前。想想安装脚本或测试运行器脚本。在bash中使用双引号引起的shell引用
只需使用
function run() {
echo "Running [email protected]"
"[email protected]"
}
不肯让我来区分run foo arg1 arg2
和run foo "arg1 arg2"
的电话,所以我需要正确逃生的论点。
我最好的拍摄迄今
function run() {
echo -n "Running"
printf " %q" "[email protected]"
echo
"[email protected]"
}
其中一期工程:
$ run echo "one_argument" "second argument" argument\"with\'quotes
Running echo one_argument second\ argument argument\"with\'quotes
one_argument second argument argument"with'quotes
,但不是很优雅。我怎样才能实现
$ run echo "one_argument" "second argument" argument\"with\'quotes
Running echo one_argument "second argument" "argument\"with'quotes"
one_argument second argument argument"with'quotes
的输出,即如何才能让printf
把周围需要报价参数引号,并在其中正确转义引号,使输出可以正确copy'n'pasted?
你可以在_all_参数周围加上引号,而不仅仅是那些需要它的?你可以绕过所有参数的双引号,如下所示:tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_03.html – kevinsa5
这将是丑陋的,如果参数包含引号,则会不正确。为了调试不那么严重的输出,但我仍然希望输出是正确的。 –
啊,我明白了。我对BASH的了解不足以帮助过去,抱歉。 – kevinsa5