Linux Shell中的echo命令中的错误? [英] Bug in echo command in linux shell?

查看:621
本文介绍了Linux Shell中的echo命令中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天注意到了一件奇怪的事情

There is weird thing i noticed today

如果我的文件名为 i o e

If i have file by name i or o or e

并在方括号brace中回显具有上述任何字符的任何字符串-[],则它始终仅打印字符

and echo any string with any of the above character within square bracet - [] then it always prints only the character

$ touch i e o

$ ls -lrh
total 0
-rw-r--r-- 1 root root 0 May 23 08:24 o
-rw-r--r-- 1 root root 0 May 23 08:24 i
-rw-r--r-- 1 root root 0 May 23 08:24 e


$ echo [offline]
e i o

$ echo [online]
e i o

$ echo [error]
e o

$ echo [soap]
o

如果我删除文件,一切正常

and if i remove the file everything works fine

$ rm -f e i o

$ ls
$ echo [offline]
[offline]

$ echo [online]
[online]

$ echo [error]
[error]

$ echo [soap]
[soap]

那么之间的关系是什么echo和这些文件名?

So what is the relation between echo and these file names ?

推荐答案

shell在命令行参数上执行路径名扩展。路径名扩展依次查看每个未加引号的参数,并尝试用匹配的文件名列表替换它。为此,以下通配符适用:

The shell performs pathname expansion on the command line arguments. Pathname expansion looks at each unquoted argument in turn and tries to replace it with a list of matching filenames. For this purpose the following wildcards apply:


  • * 表示0或更多字符,任何字符;

  • * means 0 or more characters, any characters;

表示1个字符,任何字符;

? means 1 character, any character;

[< chars> ] 表示1个字符,一个给定的< chars>。

[<chars>] means 1 character, one of the given <chars>.

如果一个或多个文件名匹配,则将命令行参数替换为匹配文件名列表。如果没有文件名匹配,则命令行参数将保持不变。

If one or more file names match, the command line argument is replaced with the list of matching file names. If no file names match, the command line argument is left as is.

因此,在您的情况下:


  • [offline] 是未引用的命令行参数,

  • [offline] is an unquoted command line argument, which

包括通配符 [ ... ]

Includes the wildcard [...], and

文件 e i o 匹配通配符,所以

The files e, i and o match the wildcard, so

shell用匹配文件名列表替换参数。

The shell replaces the argument with the list of matching file names.

道德性:总是但总是引用您不希望shell扩展的参数。总是说 echo [[offline] ,永远不要说 echo [offline]

Morality: Always but always quote the arguments which you don't want the shell to expand. Always say echo '[offline]', never say echo [offline].

这篇关于Linux Shell中的echo命令中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆