4th articles
In the previous blog, I talked about the basic concepts and initialization of variables. We know that if we initialize a variable A = Hello, we need to use echo $ A if we want to print its value.
However, there are many special variables available for $. This article describes $0 #*@_? ! -Meanings of these special variables.
Script Parameters
Just as the C language contains main (INT argc, char ** argv) and Java has main (string [] ARGs), bash can also perform operations on parameters appended to script execution.
Parameter n of the referenced script
Read a simple scriptTest. Sh:
#!/bin/bashecho $0 $1 $2 $3
Then let's take a look at my screen output.
That's right, just like what you think. The number variable 0 stores the name of the execution script, and the other numbers 1 to n saves the 1st to N parameters of the script runtime.
If no script is running, for example, directly type echo $0 in the terminal, the shell name is displayed.
Number of parameters #
Now let's continue to modify the Script test. Sh.
#! The number of parameters for/bin/bashecho "script $0 is $ #" Echo ":" Echo $1 $2 $3
The output result is:
# Represents the number of parameters. In fact, # represents the number in Bash. There is no such case. We will discuss it later.String operationYou will also see it.
All parameters * and @
Continue to modify the script
#! The number of parameters for/bin/bashecho "script $0 is $ #" Echo ":" Echo [email protected] echo $ *
Try.
Yes @, * is all variables. It seems that there is no difference between the two. But it is actually different. wildcard * treats all parameters as a variable, while @ can be understood as a set of all parameters.
Read a longer script, star_at.sh (Representing star * and [email protected])
You may not understand the for loop, but it does not matter. Here is just a simple traversal.
#! /Bin/bashecho $ * echo [email protected] echo "traversal of variables without quotation marks *" for I in $ * Do echo $ idoneecho "traversing variables with quotation marks *" for I in "$ *" Do echo $ idoneecho "traverse variables without quotation marks @" for I in [email protected] Do echo $ idoneecho "traverse variables with quotation marks @" for I in "[email protected]" Do echo $ idone
If [email protected] and $ * are not enclosed by quotation marks, there is no difference between them. However, if quotation marks exist, they are different. We can find that $ * is only a value.
Last parameter _
$ _ Saves the last parameter of a command (or script.
#! The number of parameters for/bin/bashecho "script $0 is $ #" Echo ":" Echo [email protected] echo "the last parameter is $ _"
The output result is:
Other special Variables
Exit code?
When we execute a command, we can use echo $? To view the exit code of the command.Exit code(Or return code) to determine whether the command is correctly executed. It can be understood as the return value of the command (we will also use it to return the value when learning the bash function later), but unlike other languages, it is only a fixed 8-bit binary number, that is to say, its range is 0-255. It is not as rich as the returned values in other languages. So I personally like to call itCodeInstead of values. (Of course this is not the point)
The above is a simple example. Use which to check whether a command exists. You can see it through the return value. 0 indicates success. If the value is not 0, the operation fails. This may be contrary to the ideas of other languages, but it can also be understood. After allThere is only one successful state, but there are many errors, So positive numbers are used to mark the error status.
However, usually in the script, I generally do not directly use $? To determine the return value of a command, if [] is almost the same.
PID of the current process $
Echo $ can print the PID of the current process (I will not explain what the PID is ). Continue to modify test. Sh. You only need to pay attention to the last sentence.
#! The number of parameters for/bin/bashecho "script $0 is $ #" Echo: "Echo [email protected] echo" the last parameter is $ _ "Echo" the PID of the current process is $"
The output result is:
PID of the Last Command executed in the background!
Shell has the foreground and background concepts when executing commands. The front-end is always interacting with you, but some commands will block the process, and you cannot enter other commands. For example, when a Graphical Editor gedit or Firefox browser is opened on the terminal. However, we can add an & after the command to throw it to the background, for exampleGedit &In this way, the foreground process will not be blocked. You can continue to enter other commands.
I like to start the browser on the terminal. Now let's try $ !, Can you view its PID.
About & gt;/DEB/null 2 & gt; & amp; 1 This section will be discussed in the future for input/output redirection. Its function is to throw the output of various terminals of Firefox into the waste bin, which is not displayed on the screen.
Default options of the Current Shell-
Himbh is the default option of Bash. You can use set-O to open an option or set + O to disable it. The meanings of these options are beyond the scope of this article.
- Man bash
- /-H
- /-I
- /-M
- /-B
- /-H
To view
Fun bash Script: Special Variables