Variable explanation of shell programming

Source: Internet
Author: User

Types of variables in bash

1. Local variable: The active scope is the current shell process and is not valid for Shell processes other than the current shell, including the current Shell's child shell process

Variable assignment: name= ' value '

Use reference value:

(1) Direct write character name= "root"

(2) variable reference name= "$USER"

(3) Command Reference name= ' command ', name=$ ()

Variable reference: ${name}, $name

Show all variables that have been defined: set

Delete variable: unset name

2. Environment variable: The active scope is the current shell process and its child process FFS

Variable declarations, assigning values:

Export NAME = "Value"

Declare-x name= "Value"

Variable reference: $name, ${name}

Show all environment variables:

Export

Env

printenv

Delete: unset name

3. Local variables: The effective range is the code snippet in the current shell process (usually referred to as a function)

4. Position variable: $1,$2,... To indicate that the parameters passed to it by the command line are called in the script code

5. Special variables:

$? Exit value of the last Exit program

$ A: The file name of the executed script

$*: Represents "$1c$2c$3c$4", where C is a delimited character, and the default is the SPACEBAR, which represents $ $ $ $4

[email protected]: On behalf of "$", "$", "$ $", "$4" meaning, each variable is independent, general memory [email protected]

$* and [email protected] have a difference when they're wrapped up in double quotes.

in the case of double quotation marks:

$*: Considers all parameters to be a field

[Email protected]: All parameters are separate fields

In the absence of double quotes, $* and [email protected] are the same


Example: Determining the number of rows to give a file

Linecount= "$ (wc-l |$1 |cut-d" "-f1)"

echo "has $linecount lines"

Instance:

Write a script test $* and [email protected] show the number of results

1.$* and [email protected] quoted

#!/bin/bashtest () {echo "$#"}echo ' the number of parameter in "[email protected]" is "$ (test" [email protected] ") echo ' the Number of parameter in "$*" is ' $ (test "$*")

Run results

[Email protected] shells]$ bash 2.sh a b c dthe number of parameter in "[email protected] ' is 4the number of parameter I N "$*" is 1

2..$* and [email protected] without quotation marks

#!/bin/bashtest () {echo "$#"}echo ' the number of parameter in "[email protected]" is "$ (test [email protected]) echo ' The N Umber of parameter in "$*" is ' $ (test $*)

Run results

[Email protected] shells]$ bash 2.sh a b c dthe number of parameter in "[email protected] ' is 4the number of parameter I N "$*" is 4

It can be seen from the test results that the quoted $* output is a field and the parameters of the quoted [email protected] output are separate fields


This article from "zhang1003995416" blog, declined reprint!

Variable explanation of shell programming

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.