如何在bash函数中保留引号?

时间:2018-03-22 11:45:58

标签: linux bash

函数foo以.bashrc

导出
foo()
{
    echo "run '$@'"
    $@
}
export -f foo

作品

foo cmake --build .
run 'cmake --build .'
[ 10%] Built target main
...

不起作用

foo cmake -G "Unix Makefiles" -DARCHITECTURE_TYPE=armv7_32
run 'cmake -G Unix Makefiles -DARCHITECTURE_TYPE=armv7_32'
CMake Error: Could not create named generator Unix
...

1 个答案:

答案 0 :(得分:1)

从不使用$@无引号的原因;它与$*完全相同。

foo () {
  # including $@ in a longer quoted string can cause some
  # weird side effects; I just use $* instead.
  echo "run '$*'" 
  "$@"
}