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

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?

主站蜘蛛池模板: 洪泽县| 北碚区| 吴旗县| 辽源市| 康保县| 友谊县| 琼结县| 阜新市| 清远市| 隆林| 大兴区| 开平市| 无极县| 扶沟县| 霸州市| 鹤山市| 清镇市| 新闻| 漳浦县| 滨海县| 广德县| 嘉定区| 临漳县| 安化县| 五河县| 华蓥市| 仁化县| 常宁市| 贵港市| 曲阳县| 剑阁县| 贵阳市| 确山县| 衡阳市| 黑龙江省| 怀远县| 余庆县| 阆中市| 晋城| 汤阴县| 精河县|