-1

我从 - http://www.thegeekstuff.com/2010/06/bash-conditional-expression/得到了一个脚本

这是 -

$ cat exist.sh
#! /bin/bash
file=$1
if [ -e $file ]
then
    echo -e "File $file exists"
else
    echo -e "File $file doesnt exists"
fi

$ ./exist.sh /usr/bin/boot.ini
File /usr/bin/boot.ini exists

我在 echo 附近使用了没有 -e 的相同代码,它可以工作。那么,在那里使用 -e 的目的是什么?

4

2 回答 2

4

-e标志可以解释每个 STRING 中的以下反斜杠转义字符:

\a          alert (bell)

\b          backspace

\c          suppress trailing newline

\e          escape 

\f          form feed

\n          new line

\r          carriage return

\t          horizontal tab

\v          vertical tab

\\          backslash

\NNN
      the character whose ASCII code is NNN (octal); if NNN is not
      a valid octal number, it is printed literally.

\xnnn
      the character whose ASCII code is the hexadecimal value 
      nnn (one to three digits)

来源:http ://ss64.com/bash/echo.html

于 2013-08-01T18:59:29.280 回答
1

-e可以解释反斜杠转义,但回答你的问题,关于它存在的目的,它似乎根本没有。它甚至可能是有害的。echo -e如果您想在字符串中包含那些反斜杠字符,这很有用,但在您的示例中不是这种情况,除非$file有它们,然后可能会发生这种情况:

$ touch test\\test
$ ls
exist.sh  test\test
$ ./exist.sh test\\test
File test   est exists

没有-e你得到正确的文件名。当然,这都是学术性的,因为文件不太可能包含反斜杠实体,但是我们可以得出结论,将这些开关放在那里是为了让您感到困惑。

于 2013-08-01T19:55:28.790 回答