Shell script learning: Special variables and C language format Loops

Source: Internet
Author: User

There are several special variables in shell scripts, which sometimes play a major role. Let's repeat them tonight and make a small summary.

These special variables are:

$ @ All variables are treated as a string
$ * Each is an independent string.
$ # Reference the number of parameters passed to the script through the command line
$0 script name itself
$ Process ID of the current command
$ _ Display result of execution of the last command
$! PID Number of the latest command running in the background
$-Pilot that references the options of the current command or script

 

The following small script shows the differences between them:

#!/bin/bashIFS=,echo $1echo $2echo $@echo $*echo $#echo `basename {1}`echo $!echo $-

The execution result is as follows:

[root@server45 tmp]# ./a.sh a b c d e f g h aba b c d e f g ha b c d e f g h8a.sha.sh3695hB

 

The shell script also provides a for loop similar to the C language in the following format:

For (a = 1; A <10; A ++) do

....

....

Done
Its syntax is similar to C, and its nesting syntax is as follows:
For (a = 1; A <= 3; A ++ ))
Do
...
For (B = 1; B <= 3; B ++ ))
Do
...
Done
Done

It can process and output two command lines at the same time, increasing the flexibility and scalability of the script.

For example

#!bin/bashfor (( a=10;a>0;a-- ))do   echo "hello 1"   for (( b=1;b<=10;b++ ))   do       echo "hello 2"   donedone

 

[root@server45 tmp]# ./b.sh hello 1hello 2hello 2hello 2hello 2hello 2hello 2hello 2hello 2hello 2hello 2hello 1

 

 

 

 

 

 

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.