Linux Learning notes-passing parameters to the Shell

Source: Internet
Author: User

We can pass parameters to the script when we execute the Shell script. The format for getting parameters within a script is: $n. (n represents a number, 0 is the name of the shell script executed, 1 is the first argument to execute the script, 2 is the second argument to execute the script, and so on ...). )

Examples Show

We first write a script named test.sh, whose contents are as follows:

#!/bin/BashEcho "Shell Output script name and parameters";Echo "name of script executed: $";Echo "The first parameter is: $";Echo "The second parameter is: $-a";Echo "The third parameter is: $ $";

After we give the script permission to run, we run the output:

chmod +x test. SH  $ . /test. SH 1 2 3 Shell Pass Argument instance! The file name to execute:. /test. SH The first parameter is: 1 The second parameter is: 2 The third parameter is: 3
Processing parameter Extensions

In addition to the above usage, we often use these:

parameter Handling Description
$# The number of arguments passed to the script
$* Displays all parameters passed to the script in a single string.
such as "$*" in the Case of "" ", in the form of" $ $ ... $n "output all parameters.
$$ The current process ID number for the script to run
$! ID number of the last process running in the background
[Email protected] Same as $*, but quoted when used, and returns each parameter in quotation marks.
such as "[email protected]" with "" "in the case, with" $ "" $ "... All parameters are output in the form "$n".
$- Displays the current options used by the shell, the same as the SET command function.
$? Displays the exit status of the last command. 0 means there is no error, and any other value indicates an error.

Linux Learning notes-passing parameters to the Shell

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.