Two variables and arrays of the Linux shell

Source: Internet
Author: User
Tags set set

First, what is a variable

Shell programming language is a non-type interpreted language, unlike the C++/java language programming requires the declaration of variables, the shell assigns a variable, in effect, it defines the variable, in all Linux supported Shell, you can use the assignment symbol (=) to assign a value to the variable.

Shell variables can be divided into two categories: local variables and environment variables. Local variables are only used in shell scripts that create them. Environment variables can be used in the shell in which they are created and in any child processes it derives from. Some variables are created by the user, while others are private shell variables.

For example, define the a=123 in the script, define such a variable, the preceding variable name, followed by the value of the variable.

A reference variable can use $ A, what effect will the variable put in the script? As follows:

#!/bin/bash#author Jacken 2015-04-28a=123echo "The number is $A" Execution script: Bash test.sh, the results will be displayed: the number is 123

A simple understanding of variables, equivalent to defining an alias-name, referring to the time of adding the $ symbol on it.

Ii. operation and type of variables

Defining variables

There can be no spaces between the variable name and the equals sign, which may be different from any programming language you are familiar with. At the same time, the name of the variable names must follow the following rules:

1. The first character must be a letter (a-z,a-z).

2, the middle cannot have the space, can use the underscore (_).

3. Punctuation cannot be used.

4. You cannot use the keywords in bash (you can view reserved keywords with the help command).

5. Variables are strictly case-sensitive.

Using variables

Echo Prints the line to the screen.

Use a defined variable, just precede the variable name with the dollar sign ($)

The curly braces outside the variable name are optional, plus the curly braces are used to help the interpreter identify the bounds of the variable, such as the following:

[Email protected] ~]# A=hi[[email protected] ~]# echo $Ahi [[email protected] ~]# echo $Ahello [[email protected] ~]# Echo ${a}hellohihello[[email protected] ~]#

It is a good programming habit to add curly braces to all variables.

Redefining variables

A defined variable can be redefined, such as:

[email protected] ~]# cat test.sh #!/bin/bash# #A =hiecho "The first variable is $A" A=helloecho "The Second Variabe is $A" [ [Email protected] ~]# bash test.sh The first variable is hithe Second Variabe are hello[[email protected] ~]#

Read-only variables

Use the readonly command to define a variable as a read-only variable, and the value of a read-only variable cannot be changed. The following example attempts to change a read-only variable, resulting in an error:

[email protected] ~]# cat test.sh #!/bin/bash# #A =hib=hi2readonly A ba= "Hello" b= "Hello2" echo "The first variable is $A $B" [Email protected] ~]# bash test.sh test.sh:line 7:a: readonly variabletest.sh:line 8:b: readonly variablethe First VA Riable is hi hi2[[email protected] ~]#

Delete a variable

The variable cannot be used again after it has been deleted; The unset command cannot delete a read-only variable.

[email protected] ~]# cat test.sh #!/bin/bash#a=hib=hi2readonly aunset A ba= "Hello" b= "Hello2" echo "The first variable is $A $B "[[email protected] ~]# bash test.sh test.sh:line 6:unset:a: Cannot unset:readonly variabletest.sh:line 7:a: RE Adonly variablethe First variable is hi hello2[[email protected] ~]#

Variable name = Empty to the right of the equals sign, which means that the value of the variable is set to NULL

unset variable name without any options, first try to cancel the variable, if it fails, then try to cancel the function with the same name as the variable

UNSET-V variable name -V indicates that a variable is to be canceled

The Unset-f function name-F means that the function is to be canceled

Both the "Declare-x variable name" and "Export variable name" can turn variables into environment variables.

[Email protected] ~]# a=hi[[email protected] ~]# b=hello[[email protected]  ~]# declare -x a   //A into the environment variable [[Email protected] ~]# vim  test.sh [[email protected] ~]# cat test.sh #!/bin/bash#echo  the   first variable is   $A "echo " The  second variable is    $B "[[email protected] ~]# bash test.sh the  first variable  is  hithe  second variable is   [[email protected]  ~]#  or  [[email protected] ~]# c=one[[email protected] ~]# d=two[[ email protected] ~]# export c    //C into the environment variable [[email protected]  ~]# bash[[email protected] ~]# echo  $Cone [[email protected] ~]#  echo  $D [[Email protected] ~]# 

ReadOnly variable name to set variable to read-only variable

- f function name setting the function cannot be modified

The array variable name after-a array variable-A is a reading-only group

Declare-r variable name to set variable to read-only variable

-A variable name setting variable is an array

-P variable name displays the properties of the variable

The-i variable name variable is an integer

-x variable name becomes environment variable

Alias aliases = directives

1, the equal sign can not be blank on both sides

2, the right side of the equal sign with white characters, need to use quotation marks ", the file location in the home directory. BASHRC

When you run the shell, there are three different variables:

1) Local Variables

Local variables are defined in a script or command, only valid in the current shell instance, and other shell-initiated programs cannot access local variables.

2) Environment variables

All programs, including shell-initiated programs, can access environment variables, and some programs require environment variables to keep them running properly. Shell scripts can also define environment variables when necessary.

3) Shell variables

Shell variables are special variables that are set by the shell program. Some of the shell variables are environment variables, some of which are local variables that guarantee the shell's normal operation.

Special variables

\ Escape to convert a character with special meaning to a non-representational string.

Variable meaning

The file name of the current script

$n arguments passed to the script or function. N is a number that represents the first few parameters. For example, the first parameter is $ $, and the second argument is $ A.

$# The number of arguments passed to the script or function.

$* All parameters (a whole string) passed to the script or function.

[Email protected] all arguments (strings) passed to the script or function. When enclosed by double quotation marks (""), it is slightly different from $*, as will be mentioned below.

$? The exit state of the last command, or the return value of the function.

$$ The current shell process ID. For Shell scripts, this is the process ID where the scripts are located.

[Email protected] ~]# chmod +x test.sh [[email protected] ~]#./test.sh One and three four the file name identifier of the current script./test.sh passed to the script Number $n The number of three passed to the script $# 4 all parameters passed to the script $* one and one three four all parameters passed to the script [email protected] One and one three four the status return value of the previous command? 0 Current shell process id$$ 17820[[email protected] ~]# cat test.sh #!/bin/bash#echo "filename identifier of the current script \$0 $" echo "parameters passed to script \ $n $" ech O "number of passes to script \$# $#" echo "All parameters passed to the script \$* $*" echo "All parameters passed to the script \[email protected] [email protected]" echo "The status return value of the previous command \$? $?" echo "Current shell process id\$$ $$" [[email protected] ~]#

The difference between $* and [email protected]

From the above example, it looks like $* and [email protected] output is the same, in fact, $* output is "one of the three four" a whole string, and [e-mail protected] output is a single, and the three , four four strings.

Other

In writing the script, in order to be afraid of the wrong variable name, resulting in the difficulty of troubleshooting, we can stipulate that: all variables must be set before the program can be used.

Shopt-s-O Nounset

-S is the meaning of the open option;

-O refers to the option that can be set Set-o

Nounset indicates that a variable must be set before it can be used

Three, array

An array is a data structure that consists of related items. Each data item is called an element called an array, and the value of each element can be obtained by using an index.

The array of bash has no limit on the number of elements, the index of the array starts with 0, but it does not have to be contiguous (can be skipped).

Displays or gets the value of an element ${array [index value]}, and the 3rd element of array A is ${a[2]}. An index can do arithmetic operations.

When using an index of an array, the first element starts with 0 and the index value of the nth element is n-1

echo ${array [@]} takes out all the elements and gets the characters separated by spaces

echo ${Array [*]} takes out all the elements and gets a string of strings

echo${#数组 [@]} or echo ${#数组 [*]} to get the number of array elements

unset array cancellation array

unset array [2] cancels the 3rd element in the array

[[email protected] ~]# cat test.sh #!/bin/bash#a[0]=5a[1]=15a[3]=17a[4]=19echo "\${a[@]} represents all elements of array A (a space-separated string) ${a[@]}" echo "\${a[*"} represents all elements of array a (representing a whole string) ${a[*]} "echo" \${#A [@]} represents the number of elements of array a ${#A [@]} "echo" \${#A [*]} represents the number of elements of array a ${#A [*]} "[[ Email protected] ~]# [[email protected] ~]# bash test.sh ${a[@]} represents all elements of array A (a space-separated string) 5 [19${a[*]} represents all elements of array a (representing a whole string ) 5 19${#A [@]} represents the number of elements of array a 4${#A [*]} represents the number of elements of array a 4[[email protected] ~]#

This article is from the "Welcome to Linux World" blog, so be sure to keep this source http://linuxnote.blog.51cto.com/9876511/1641142

Two variables and arrays of the Linux shell

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.