Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 这个脚本中-e标志的用途是什么?_Linux_Bash_Unix - Fatal编程技术网

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

Linux 这个脚本中-e标志的用途是什么?,linux,bash,unix,Linux,Bash,Unix,我收到了一份来自- 是的- $ 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,它可以工作。那么,在那里使用

我收到了一份来自-

是的-

$ 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的目的是什么?

使用-e标志可以解释以下反斜杠: 每个字符串中的字符:

\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)
来源:

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

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

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

您阅读文档了吗?这应该永远是您的第一个操作。-e启用反斜杠转义的解释