Fun Bash Script: Special Variables

Source: Internet
Author: User

Fun Bash Script: Special Variables

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.

Expires + yOe5 + 8O709C9xbG + expires + 7PGoaM8L3A + expires + CjxwPs/expires + NDeuMTSu8/timeout + PC9wPgo8cD48L3A + expires "brush: java;"> #! The number of parameters for/bin/bashecho "script $0 is $ #" echo ":" echo $1 $2 $3The 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 $ @ 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 @)

You may not understand the for loop, but it does not matter. Here is just a simple traversal.

#! /Bin/bashecho $ * echo $ @ echo "traverse variables without quotation marks *" for I in $ * do echo $ idoneecho "traverse variables with quotation marks *" for I in" $ * "do echo $ idoneecho" traverse variables without quotation marks @ "for I in $ @ do echo $ idoneecho" traverse variables with quotation marks @ "for I in" $ @" do echo $ idone

No. When traversing, we can find that if $ @ and $ * are not surrounded by quotation marks, there is no difference between the two. 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 $ @ 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 $ @ echo" the last parameter is $ _ "echo" the current process PID 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

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.