Shell Special variables

Source: Internet
Author: User

Next look at some special variables
$? : Receives the return status code of the previous command, so that you know whether the previous command was successful,
If the previous command succeeds, the return status code is 0, and if the previous command fails, the return value is a value between 1-255, that is, the return value of the success is only one, and the return value of the failure is various.

Execute the LS command and then execute echo $? Status code is 0
Execute the LKKK command, and then execute echo $? Status code is 127





$#: Number of parameters
In general, when we execute some scripts, if the number of parameters specified later is not enough, then the script will give a hint, saying that the number of parameters is not enough, then it is how to judge it. is to use $ #来判断的.
For example
Write a test.sh script that writes the following code
Echo $#

This returns a value of 0 when the test.sh script is called and if a few arguments are specified, the return value is just a few.


$* [email protected]: Get all parameters specified later in the script
Both commands can get all the parameters after the specified script, where the execution results of the two commands are consistent and specific, and are analyzed when they are used.

Write a test.sh script
Echo $*
echo [email protected]
In this way, when executing the test.sh script, specify parameters later, Test.sha b C will print two times a B c parameter, indicating that the execution results of the two commands are consistent.


$$: Gets the process number of the current shell,
What's the use of this thing, this can be used to implement shell process suicide.

Using the KILL command in a shell script, follow this command to implement a process suicide.
For example:
Echo $$
echo "Start"
Kill $$
Sleep 900000000
echo "End"

Or you can exit the shell script using exit

For example, replace the kill $$ above with exit [num] (num indicates exit status code, can be specified or not specified, default is 0)

For more highlights, please follow: http://bbs.superwu.cn

Focus on Superman Academy QR Code: 650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M00/6D/17/wKioL1VcUt3iGEReAADAdZasjL0734.jpg " Title= "Superman Academy. jpg" alt= "wkiol1vcut3igereaadadzasjl0734.jpg"/>

Shell Special variables

Related Article

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.