Linux lakes and 10:bash the aesthetics and philosophy in script programming language

Source: Internet
Author: User

I confess that I once again became the title party. But admittedly, this must be an essential essay. In this article, I'll explore the aesthetics and philosophy of the Bash scripting language. This is not a tutorial for bash scripting, but it gives you a more in-depth look at Bash scripting programming and faster learning of bash scripting. Reading this essay does not require you to have experience with bash programming, but be as passionate about exploring the nature of programming languages as I do, and feeling their charms.

I wanted to write something about bash long ago. A few days ago saw a Bo friend in the yard to Learn bash experience (here http://www.cnblogs.com/viroyiheng/p/3988787.html), mentioned that the variable is not preceded by a "$", and the use of the variable is preceded by a "$", is similar to an lvalue right value in other programming languages. At that time I could not help replying to a moment. I pointed out that the existence of "$" is not to differentiate the lvalue right value, but to expand the variable at the position where the variable is located. I also point out that bash can be seen as a string-oriented programming language. This is the nature of the Bash scripting language: Everything is a string. All the philosophies of the Bash scripting language revolve around strings: Where do they come from? Where to? What are you doing? All the aesthetics of the Bash scripting language are derived from strings: almost all of the symbols on the keyboard "$, ~ 、!、 #, &, (,), [,], {,}, |, >, < 、-、.、,、,, *, @, ', ', ', ') An extremely complex string of visually appealing features that are arranged together.

First, everything is a string

Bash is a Shell,shell the original intention is to glue the various tools in the system together, so its most fundamental function is to invoke various commands. But Bash also offers rich programming features. We often classify programming languages, such as process-oriented languages, object-oriented languages, function-oriented languages, and so on. You can think of the Bash scripting language as a string-oriented language. The essence of the Bash language is that everything is a string. Look at the variables in these:

Is some of the demos I did in the Interactive Bash command line. In, I assign a value to the variable, whether the right side of the equal sign is a string without quotation marks, a string with quotation marks, or even a number, or a mathematical expression, the final result is that the variables are stored in strings. I use a For loop to show all the variables, and I can see that the mathematical expression is stored as a string and is not evaluated.

Two, references and metacharacters

If everything is a trivial string with no special features, it cannot form a programming language. In bash, there are a number of symbols with special meanings, such as the "$" symbol being used for string expansion, and the "&" symbol for the command to execute in the background, "|" Use as a pipe, > < for input and output redirection, and so on. So in bash, although it's also a string, the quoted string is not the same as the string enclosed by quotation marks, and the string enclosed in quotation marks is not the same as the string surrounded by double quotes.

What is the difference between a quoted string and a string without a quoted number? Are some of the more typical examples I've built:

In, I showed 7 ways to generate a string in bash: Curly brace expansion, tilde expansion, parameter expansion, command substitution, arithmetic expansion, word segmentation, and file path expansion. There are also two ways to generate strings that are not spoken (Process substitution and History commands are expanded). When you're programming with bash scripting, it's enough to understand how the 7 types of strings are generated. When using the command line, it is easier to understand the history command deployment.

As you can see in the above picture, there are some unfolding methods that are not useful in strings surrounded by double quotes, such as curly brace expansion, tilde expansion, word segmentation, and file path expansion, but only parameter expansion, command substitution, and arithmetic expansion work. As you can see from the picture, parameter expansion, command substitution, and arithmetic expansion in a string are guided by the "$" symbol, and command substitution can also be guided by "'". Therefore, it can be further summed up that in a string surrounded by double quotes, only the "$, ', \" Three characters have special meanings.

If you want any one character to have no special meaning, enclose the string in single quotation marks. When using regular expressions, for example, when using tools such as SED or awk, the commands of SED and awk tend to be surrounded by single quotes because they often contain many special characters. For example, using the awk command to display the user name and full name of each user in the/etc/passwd file, you can use this command, awk -e '{print $1,$5}' , where the command passed to Awk is surrounded by single quotes, stating that bash does not perform any of these substitutions.

Another special character is "\", which is also a reference. It can remove the special meaning (reference) of a special character immediately following it, or it can have a special meaning (escape character) for a normal character immediately following it. The need for "\" exists because in bash, some characters are called metacharacters, and once they occur, a string is split into multiple strings. If you need to include these metacharacters in a string, you must refer to them. Such as:

The most common meta characters are spaces. As can be seen from the above pictures, if you want to assign a string containing a space to a variable, either enclose the string in double quotation marks, or use "\" to reference the space. As you can see, there are only 9 bash, which are "|, &, (,),;, <, >, Space, Tab", and the meta-characters that often appear in other programming languages "., {,}, [,]" and subtraction as a mathematical operation, is not a meta character in bash.

Three, where does the string come from, where to go

After describing the string, describing the references and metacharacters, the next goal is to explore the philosophical question: Where does the string come from and where to go? Through the discussion of this philosophical question, we can deduce the whole syntax of the Bash scripting language. Where does the string come from? Obviously, one of the very direct sources is that we knocked on the keyboard. In addition, the 789 kinds of string expansion methods I mentioned earlier.

The process of string expansion is as follows:

1. First use a meta-character to split a string into multiple strings;

2. If the string is used to assign a value to a variable, it is considered to be surrounded by double quotes, regardless of whether it is surrounded by double quotes;

3. If the string is not enclosed in quotation marks and double quotes, the curly brace expands, and the {a,b}c expands to AB AC;

The above three processes can be proven by:

4. If the string is not enclosed in quotation marks or double quotes, the tilde is expanded, the ~/is expanded to the user's home directory, ~+/is expanded to the current working directory (PWD), ~-/is expanded to the previous working directory (OLDPWD);

5. If the string is not enclosed in quotation marks, the arguments and variables are expanded; This type of expansion begins with "$", which is the most complex of the entire bash string expansion, including user-defined variables, including all environment variables, both of which are "$" followed by variable names, and also include positional variables "$ 1, $ 、...、 $9 、... ", other special variables:" [email protected], $*, $#, $-, $!, $, $, $_, and even the array: "${var[i]}", you can also perform various complex operations on strings during the unfolding process, such as: " ${parameter:-word}, ${parameter:=word}, ${parameter:+word}, ${parameter:?word}, ${parameter:offset}, ${parameter:o Ffset:length}, ${!prefix*}, ${[email protected]}, ${name[@]}, ${!name[*]}, ${#parameter}, ${parameter#word}, ${ parameter# #word}, ${parameter%word}, ${parameter%%word}, ${parameter/pattern/string}, ${parameter^pattern}, ${ Parameter^^pattern}, ${parameter,pattern}, ${parameter,,pattern} ";

6. If the string is not enclosed in quotation marks, the command substitution is made, and the command substitution has two formats, one is $ (...), the other is ' ... '; that is, the output of the command as the content of the string;

7. If the string is not enclosed in quotation marks, the arithmetic expansion is performed, and the format of the arithmetic expansion is $ (...). ;

8. If the string is not enclosed in quotation marks or double quotes, the word is split;

9. If the string is not enclosed in quotation marks or double quotation marks, the file path is expanded;

10. When the above process is complete, the quotation marks (if any) outside the string are finally removed. The above process is done only once in the order above. For example, do not expand the curly braces after the variable is expanded. If you need to go through the process again, please use Eval.

After discussing where the string came from, let's see where the string goes. That is how to use these strings. There are several ways to use the string:

1. Take it as a command execution; This is the most fundamental use of bash, after all, the shell exists to glue the various commands. If a string appears where the command should appear (such as at the beginning of a line, or after the keyword then, do, etc.), it will be executed as a command, and if it is not a valid command, it will be an error;

2. Take it as an expression; there is no expression in bash, but there is ((...)) and [[...]], there is an expression; ((...)) You can think of the string in it as an arithmetic expression, and [[...]] It will take the string inside it as a logical expression, only these two special cases;

3. Assigning a value to a variable; this is also a special case that undermines the integrity of the syntax philosophy of the Bash programming language. Why do you say that? Because "=" is not a meta-character, there are no spaces on either side, and only the 1th equals sign is treated as an assignment operator.

The following image gives evidence for the above view:

(To be continued, now go to play basketball and try to finish it tonight.) )

  

Linux lakes and 10:bash the aesthetics and philosophy in script programming language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.