Shell scripts from getting started to complex three (passing parameters)

Source: Internet
Author: User

when executing a Shell script, parameters can be passed to the script, and the format of the parameters obtained in the script is: $n. n represents a number, 1 is the first argument to execute the script, and 2 is the second parameter that executes the script ...


The following example we pass three parameters to the script and output separately, where the file name is executed

# Cat Test.sh

#!/bin/bash

echo "Shell pass parameter Example";

echo "File name: $";

echo "First parameter: $";

echo "second parameter: $";

echo "The third parameter: $ A";


Output results

# sh test.sh 1 2 3

Shell Pass-Through parameters example

File name: test.sh

First parameter: 1

Second parameter: 2

Third parameter: 3


The following special characters are used to handle parameters:

Parameter handling instructions

$# 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".

$- shows the current options that the shell uses, as is 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.


$* and [email protected] Difference:

The same point: all parameters are referenced.

Different points: only in double quotes. Assuming that three parameters 1, 2, 3 are written when the script is run, "*" is equivalent to "1 2 3" (one parameter is passed), and "@" is equivalent to "1" "2" "3" (three parameters are passed).


The following example:

# VI test.sh

#!/bin/bash

For i in "$*";d o

Echo $i

Done


echo "------------"


For i in "[email protected]";d o

Echo $i

Done


Output

# sh test.sh 1 2 3

1 2 3

------------

1

2

3


Shell scripts from getting started to complex three (passing parameters)

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.