Shell introduction series (I): Variables



The variables in the shell are weakly typed and can be used as in PHP. The difference from PHP variables is that the variables in the function are global variables by default. At the same time, there is the concept of environment variables
When a shell script executes, you can pass variables into the script

Variable definition

  • Definition method: there can be no space next to the equal sign
  • Variable name: variable names can only contain English letters / numbers / underscores. Variable names cannot start with numbers and cannot use keywords in bash
    #Define variables
    #Read only variable. The variable value cannot be modified. If it is modified, an error will be reported
    Readonly gender = 'male'
    #Variables are used with curly braces to identify variable boundaries, such as string splicing. Skill = Java echo '${skill} script'
    echo $name
    echo ${age}
    #If the variable is destroyed, unset cannot delete the read-only variable, and an error will be reported
    unset age
    unset gender
    echo $age
    #Variable modification

Variable type

There are three types of variables in the shell: string, number and array

1. String

There are two kinds of strings: single quotation marks and double quotation marks, which are similar to PHP. There can be variables and escape characters in double quotation marks

String variable definition

#Defining a string variable with single quotation marks will output Hello $name as it is
str1='hello $name'
echo $str1
#Using double quotation marks to define a string variable will output Hello Michael
str2=''hello $name"
echo $str2
  • String operation

2. Figures

3. Array

Definition of array

Definition of array: array_ name=(value0 value1 value2 value3)

#Define an array
my_ Skill = ('php ',' laravel ',' MySQL ',' Linux ',' HTML / CSS', 'other technologies')
#List the files and folders under / etc and assign them to etc as an array_ File variable and traverse the array
etc_files=`ls /etc`
for f in $etc_files; do
  echo $f
##The above example can use for file in $(LS / etc)
#Use of array todo

Array operation

#Define an array
my_ Skill = ('php ',' MySQL ',' redis', 'Linux', 'JavaScript', 'HTML', 'other skills')
#Traversal array
for k in ${my_skill[*]};
  echo $k

#Read array elements
The first element in the echo 'array is' ${my_skill [0]}
#Modify the first element of the array (you can also add elements using this method)
#Gets all elements of the array, displayed as a string
Echo 'modified my_ The skill array (displayed as a string) is' ${my_skill [*]}
#Add new element to array
Echo 'modified my_ The skill array (displayed as a string) is' ${my_skill [*]}

Scope of variable

The scope of a variable indicates the effective range of the variable

1. Environmental variables

#System environment variable / etc / profile
echo $PATH;
#Variables that can be used in child processes

2. Shell script variables

The variables defined in the script are destroyed upon exiting

Example 1: calling variables defined in the function outside the function

#Variables defined in the calling function outside the function
    #Define or reassign a global variable, which is available outside the function after the function is executed
    #Set a local variable inside a function
    local b=2
echo $a
#No output below
echo $b

Output 1

Example 2: calling variables defined outside the function within the function

#Variables defined in the calling function outside the function
    #Set a local variable
    local b=2
    echo $b
echo $b

Output 2 3

Variables outside the script

Parameters attached when running shell commands
Run. / ‘hello world’, ‘Michael’

#The script executes the parameters attached to the command
Filename of echo script: '$0'
First parameter outside echo script: '$1'
Second parameter outside echo script: '$2'
'number of parameters of echo script:'$#
Echo 'all parameters passed to the script, and the string displays:'$*
Echo 'ID number of currently running script:'$$
echo "[email protected]"


File name of the script: / usr / local / SH /
The first parameter outside the script: Hello World
The second parameter outside the script: Michael
Number of script parameters: 2
For all parameters passed to the script, the string displays: Hello World Michael
ID number of currently running script: 10087
hello world michael

This work adoptsCC agreement, reprint must indicate the author and the link to this article