这个脚本中-e标志的用途是什么?

时间:2013-08-01 18:55:49

标签: linux bash unix

我从 - 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

我使用相同的代码而没有-e靠近echo并且它有效。那么,在那里使用-e的目的是什么?

2 个答案:

答案 0 :(得分:2)

-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

答案 1 :(得分:1)

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

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

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