0

I'm trying to make a function in my ~/.bashrc file that uses an echo command to pass its arguments through the pipeline. It works, but when I try to input a '\' character it disappears. If I type \\ (two '\') it succeeds. This heppens even with -E option...

So, how can I make the code below prints "foo\bar" instead of "foobar"?

func()
{
    echo -E "${@}"
}
4

2 回答 2

2

不是echo解释反斜杠的人,而是shell。它甚至在你的函数被调用之前就被解释了。正确的解决方案是引用函数的参数。

func()
{
    echo -E "${@}"
}

func 'foo\bar'
于 2013-05-19T17:31:35.847 回答
0

您需要确保您确实为函数提供了反斜杠;该函数本身工作正常,如: jlaiho@LT2:~$ fun() { echo -E "$@"; }
jlaiho@LT2:~$ fun 'foo\bar'
foo\bar

然而,如果反斜杠甚至没有到达函数,它就不会被打印出来:
jlaiho@LT2:~$ fun foo\bar
foobar

于 2013-05-19T17:33:39.977 回答