PASS Parameters in Shell: [reprint]. PASS Parameters in shell.

Source: Internet
Author: User

PASS Parameters in Shell: [reprint]. PASS Parameters in shell.
PASS Parameters through Shell

When executing a Shell script, we can pass parameters to the script. The parameters obtained in the script are in the following format:$ N.NRepresents a number, 1 is the first parameter for executing the script, 2 is the second parameter for executing the script, and so on ......

Instance

In the following example, we pass three parameters to the script and output them separately.$0Is the execution File Name:

#! /Bin/bash # author: cainiao tutorial # url: www. runoob. comecho "Shell passing parameter instances! "; Echo" executed file name: $0 "; echo" first parameter: $1 "; echo" second parameter: $2 "; echo "third parameter: $3 ";

Set the executable permission for the script and execute the script. The output result is as follows:

$ Chmod + x test. sh $./test. sh 1 2 3 Shell passing parameter instances! Name of the file to be executed:./test. sh the first parameter is: 1 The second parameter is: 2 The third parameter is: 3

In addition, there are several special characters used to process parameters:

Parameter Processing Description
$ # Number of parameters passed to the script
$ * Display All parameters passed to the script with a single string.
For example, when "$ *" is included in... $ N "to output all parameters.
$ ID of the current process that the script runs
$! ID of the last process running in the background
$ @ It is the same as $ *, but it is enclosed in quotation marks and each parameter is returned in quotation marks.
For example, when "$ @" is included in "... All parameters are output in the form of "$ n.
$- Displays the current options used by Shell, which have the same function as the set command.
$? Displays the exit status of the last command. 0 indicates no error, and any other value indicates an error.
#! /Bin/bash # author: cainiao tutorial # url: www. runoob. comecho "Shell passing parameter instances! "; Echo" the first parameter is: $1 "; echo" the number of parameters is: $ # "; echo" The passed parameters are displayed as a string: $ *";

Run the script and the output result is as follows:

$ Chmod + x test. sh $./test. sh 1 2 3 Shell passing parameter instances! The first parameter is: 1. The number of parameters is: 3. The passed parameter is displayed as a string: 1 2 3.

Difference between $ * and $:

  • Similarities: All parameters are referenced.
  • Difference: It is only reflected in double quotation marks. If three parameters 1, 2, and 3 are written during the script running, "*" is equivalent to "1 2 3" (passing a parameter ), "@" is equivalent to "1" "2" "3" (three parameters are passed ).
#! /Bin/bash # author: cainiao tutorial # url: www. runoob. comecho "-- \ $ * demo ---" for I in "$ *"; do echo $ idoneecho "-- \ $ @ demo ---" for I in "$ @"; do echo $ idone

Run the script and the output result is as follows:

$ Chmod + x test. sh $./test. sh 1 2 3 -- $ * demo --- 1 2 3 -- $ @ demo --- 123

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.