The basics of how to use Linux (如何使用Linux,基础笔记八)

In this blog, we are going see some interesting and complex features of shell, but we just use one command in this exploring process.

  1. What is the “expansion” ?

Each time you type a command line and press the enter key, bash performs several processes upon the text before it carries out your command. We have seen a couple of cases of how a simple character sequence, for example “*”, can have a lot of meaning to the shell. The process that makes this happen is called expansion.

With expansion, you type something and it is expanded into something else before the shell acts upon it. To demonstrate what we mean by this, let’s take a look at the echo command. echo is a shell builtin that performs a very simple task. It prints out its text arguments on standard output.

e.g.
在这里插入图片描述
That’s pretty straightforward. Any argument passed to echo gets displayed.

e.g.在这里插入图片描述

As you recall from our work with wildcards, the “” character means match any characters in a filename, but what we didn’t
see in our original discussion was how the shell does that. The simple answer is that the shell expands the “
” into something else (in this instance, the names of the files in the current working directory) before the echo command is executed. When the enter key is pressed, the shell automatically expands any qualifying characters on the command line before the command is carried out, so the echo command never saw the “*”, only its expanded result. Knowing this, we can see that echo behaved as expected.

1.1. Pathname expansion

The mechanism by which wildcards work is called pathname expansion, and let’s see some examples.

e.g. First, go to the home directory, and then print out all files’name which start with the “D”
在这里插入图片描述

e.g. print out all files’name which end with the “s”
在这里插入图片描述

e.g.print out all files’name which start with the uppercase letter
在这里插入图片描述

e.g. Print out all pathnames which start with “/usr/” and end with “/share”.
在这里插入图片描述

Tips:
Cite from Linux command line

1.2. Tilde expansion : “~”

The tilde character (“~”) has a special meaning. When used at the beginning of a word, it expands into the name of the home directory of the named user, or if no user is named, the home directory of the current user.

e.g. print out the home directory of current user and named user
在这里插入图片描述

1.3. Arithemtic expansion

The shell allows arithmetic to be performed by expansion. This allow us to use the shell prompt as a calculator.

Usage:
在这里插入图片描述
e.g.
在这里插入图片描述

Arithmetic expansion only supports integers (whole numbers, no decimals), but can perform quite a number of different operations.

Tips:
Cite from Linux command line

e.g.在这里插入图片描述
Spaces are not significant in arithmetic expressions and expressions may be nested.

1.4. Brace expansion

Perhaps the strangest expansion is called brace expansion. With it, you can create
multiple text strings from a pattern containing braces.

Patterns to be brace expanded may contain a leading portion called a preamble and a trailing portion called a postscript. The brace expression itself may contain either a comma-separated list of strings, or a range of integers or single characters. The pattern may not contain embedded whitespace.

e.g.
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

Actually we can use this feature with other commands.

e.g. Make directories
在这里插入图片描述
1.5. Parameter expansion
It’s a feature that is more useful in shell scripts than directlyon the command line. Many of its capabilities have to do with the system’s ability to store small chunks of data and to give each chunk a name. Many such chunks, more properly called variables, are available for your examination.

For example, the variable named “USER” contains your user name. To invoke parameter expansion and reveal the contents of USER you would do this.

e.g.
在这里插入图片描述

e.g. To see a list of available variables in the system environment
在这里插入图片描述
在这里插入图片描述

if you mistype a pattern, the expansion will not take place and the echo command will simply display the mistyped pattern. With parameter expansion, if you misspell the name of a variable, the expansion will still take place, but will result in an empty string.

e.g.
在这里插入图片描述

  1. Command substitution

Command substitution allows us to use the output of a command as an expansion.

e.g.
在这里插入图片描述

在这里插入图片描述
Here we passed the results of which cp as an argument to the ls command, thereby getting the listing of of the cp program without having to know its full pathname. We are not limited to just simple commands.

在这里插入图片描述
In this example, the results of the pipeline became the argument list of the file command.

  1. Quoting

在这里插入图片描述
In the first example, word-splitting by the shell removed extra whitespace from the echo command’s list of arguments. In the second example, parameter expansion substituted an empty string for the value of “$1” because it was an undefined variable. The shell provides a mechanism called quoting to selectively suppress unwanted expansions.

3.1. Double quotes

The first type of quoting we will look at is double quotes. If you place text inside double quotes, all the special characters used by the shell lose their special meaning and are treated as ordinary characters. The exceptions are “$”, “\” (backslash), and “`” (backquote). This means that word splitting, pathname expansion, tilde expansion, and brace expansion are suppressed, but parameter expansion, arithmetic expansion, and command substitution are still carried out.

Using double quotes, we can cope with filenames containing embedded spaces. Say we were the unfortunate victim of a file called two words.txt. If we tried to use this on the command line, word-splitting would cause this to be treated as two separate arguments rather than the desired single argument.

e.g.
在这里插入图片描述
在这里插入图片描述

Remember, parameter expansion, arithmetic expansion, and command substitution still take place within double quotes.

e.g.
在这里插入图片描述

3.2. Single quote

If we need to suppress all expansions, we use single quotes. Here is a comparison of unquoted, double quotes, and single quotes.

e.g.
在这里插入图片描述

  1. Escaping characters

Sometimes we only want to quote a single character. To do this, we can precede a character with a backslash, which in this context is called the escape character.

e.g.
在这里插入图片描述

It is also common to use escaping to eliminate the special meaning of a character in a filename.

在这里插入图片描述

Tips:

Cite from Linux command line

在这里插入图片描述

发布了20 篇原创文章 · 获赞 3 · 访问量 259

猜你喜欢

转载自blog.csdn.net/qq_34515959/article/details/99432932