Special parameter variables in Linux

Source: Internet
Author: User

    1. Parameter count

Special Variable $ #可以存储执行脚本时包含的命令行参数个数!

#!/bin/bash

If [$#-ne 2]

Then

Echo Usage:input a B

Else

Total=$[$ + $]

echo the total is $total

Fi


[Email protected] ~]#./test33.sh 3 4

The total is 7

****************************************************

When you want to get the $ #参数的最后面那个参数时, you can use the ${!#} command

#!/bin/bash

params=$#

echo the toal parameters is $params

echo the last parameters is ${!#}


[Email protected] ~]#/test34.sh 3

The Toal parameters is 1

The last parameters is 3

****************************************************

Get all data

$* all arguments provided on the command line as a single word that contains each parameter value that appears on the command line

[email protected] handles all the arguments in the command line as multiple words in the same string


#!/bin/bash

#testing $* and [email protected]

echo "Using The \$* method: $*"

echo "Using the \[email Protected] method: [Email protected]"


Count=1

For parm in "$*"

Do

echo "\$* Parameters # $count = $parm"

count=$[$count + 1]

Done



Count=1

For parm in "[email protected]"

Do

echo "\[email protected] Parameters # $count = $parm"

count=$[$count + 1]

Done


[Email protected] ~]#/test35.sh Jack Tom bot luck haha

Using the $* method:jack Tom bot luck haha

Using the [email protected] method:jack Tom bot luck haha

$* Parameters #1 = Jack Tom bot luck haha

[Email protected] Parameters #1 = Jack

[Email protected] Parameters #2 = Tom

[Email protected] Parameters #3 = bot

[Email protected] Parameters #4 = Luck

[Email protected] Parameters #5 = haha

$*: As a word

[Email protected]: treat each parameter separately

Special parameter variables in Linux

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.