Shell Foundation--shell Special variables

Source: Internet
Author: User

A. Variable list

Two. Experiments

System: CentOS 7

1. Special variables

[Email protected] ~_~ day2]# cat p.sh
#!/bin/bash
Echo ' $: ' $
Echo ' $*: ' $*
Echo ' [email protected]: ' [email protected]
Echo ' $#: ' $#
Echo ' $1$2$3: ' $1$2$3
[[email protected] ~_~ day2]# sh p.sh a b c D
$0:p.sh
$*:a b c D
[email protected]:a b c D
$#:4
$1$2$3:abc

2.$$

[Email protected]~~ day2]# Echo $$
1974

3.shift command: shift position parameter, rename positional parameter $N + 1, $N +2 ... to $ ... If no N is given, it is assumed to be 1. $ #会少N after each move

Exit Status: Returns success, such as n is negative or greater than $ #则失败

(1). Example one, move position variable

[Email protected] ~_~ day3]# cat r.sh
#!/bin/bash
Until [$#-eq 0]
Do
echo "The first value is:$1 the number of values is:$#"
Shift
Done
[[email protected] ~_~ day3]# sh r.sh 1 2 3 4 5
The first value is:1 the number of values Is:5
The first value is:2 the number of values Is:4
The first value is:3 the number of values Is:3
The first value is:4 the number of values Is:2
The first value is:5 the number of values Is:1

(2). Example two, using shift shift to accumulate

[Email protected] ~_~ day3]# cat s.sh
#!/bin/bash
Sum=0
Until [$#-eq 0]
Do
sum= ' expr $sum + $ '
Shift
Done
echo "sum is: $sum"
[[email protected] ~_~ day3]# sh s.sh 1 2 3 4 5
Sum is:15

Note: The above displacement is 1, the displacement is greater than 1 o'clock, the number of input parameters are different, there may be a logic error.

Shell Foundation--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.