官术网_书友最值得收藏!

  • Bash Quick Start Guide
  • Tom Ryder
  • 313字
  • 2021-07-23 16:58:56

Shell metacharacters

So far, all of our examples of commands and arguments have been single unquoted shell words. However, there is a set of metacharacters that have a different meaning to Bash, and trying to use them as part of a word causes problems.

For example, suppose you want to create (touch) a new file named important file. Note that there's a space in the name. If you try to create it as follows, you get unexpected results:

$ touch important file

If we list the files in the directory after running this, using ls -1 to put all the names on separate lines, we can see we've actually created two files; one named important, and one named file:

$ ls -1
file
important

This happened because the space between the two words separated them into two separate arguments. Space, tab, and newline are all metacharacters. So are | (pipe), & (ampersand), ; (semicolon), ( and ) (parentheses), and < and > (angle brackets).

There are many other characters interpreted specially by Bash in some contexts, including {, [, *, and $, but these are not considered metacharacters according to the manual page's definition.

Even the error messages can be confusing if you try to use a word with one of these characters in it:

$ touch Testfile<Tom>.doc
bash: Tom: No such file or directory

$ touch Review;Final.doc
bash: Final.doc: command not found

In some cases, you may not get an error message at all, and something very unexpected will happen instead; for example:

$ touch $$$Money.doc
$ ls
31649.doc

A lot of the time we can simply work with files and words that don't use these characters. However, we can't always do that, and we can't just hope others behave the same way—we will eventually have to work with their files and data. How can we include special characters in our words safely?

主站蜘蛛池模板: 灵武市| 秀山| 饶河县| 濉溪县| 呼伦贝尔市| 岗巴县| 双流县| 丰镇市| 得荣县| 平湖市| 拉萨市| 吉木萨尔县| 平昌县| 阿城市| 岐山县| 九龙城区| 瓦房店市| 盖州市| 昭平县| 鄂托克前旗| 德保县| 绥阳县| 都兰县| 秦安县| 永年县| 鄂伦春自治旗| 武平县| 丁青县| 昆明市| 威宁| 威信县| 陕西省| 营山县| 临城县| 阿巴嘎旗| 铁岭市| 哈密市| 华池县| 西峡县| 化州市| 翁源县|