Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The shell variable


May 22, 2021 Linux


Table of contents


The shell variable

When defining a variable, the variable name is not using the dollar sign ($, as required by the variable in the PHP language), such as:

your_name="w3cschool.cn"

Note that there can be no space between the variable name and the equal sign, which may be different from all the programming languages you are familiar with. A t the same time, variable names are named according to the following rules:

  • The first character must be the letter (a-z, A-Z).
  • There can be no spaces in the middle, and you can use underscores.
  • Punctuation cannot be used.
  • Keywords in bash cannot be used (retention keywords can be viewed using the help command).

In addition to explicitly assigning values directly, you can assign variables with statements such as:

for file in `ls /etc`

The above statement loops out the file names of the directories under /etc.


Use variables

Using a defined variable, simply add a dollar sign to the name of the variable, such as:

your_name="qinjx"
echo $your_name
echo ${your_name}

The braces outside the variable name are optional, plus all lines, and are added to help the interpreter identify the boundaries of the variable, as in the following case:

for skill in Ada Coffe Action Java ; do
    echo "I am good at ${skill}Script"
done

If you don't put braces on the skill variable and write it "I am good at $skillScript", the interpreter will treat $skillScript as a variable (its value is empty), and the code execution results will not be what we expected.

It is recommended to put braces on all variables, which is a good programming habit.

Defined variables that can be redefined, such as:

your_name="tom"
echo $your_name
your_name="alibaba"
echo $your_name

This is legal to write, but note that you can't write $your_name-"alibaba" the second assignment, and only add a dollar character ($) when using variable$.


The shell string

Strings are the most commonly used and useful data types in shell programming (there are no other types to use except numbers and strings), and strings can be quoted in single or double quotes, or without quotation marks. T he difference between single and double quotes is similar to PHP.

Single quotes

str='this is a string'

Limits for single quote strings:

  • Any character in a single quote is output as is, and the variables in a single quote string are invalid;
  • Single quotes do not appear in single quote strings (not after using escapes for single quotes).

Double quotes

your_name='qinjx'
str="Hello, I know your are \"$your_name\"! \n"

Benefits of double quotes:

  • Variables can be in double quotes
  • Escape characters can appear in double quotes

Stitch the string

your_name="qinjx"
greeting="hello, "$your_name" !"
greeting_1="hello, ${your_name} !"
echo $greeting $greeting_1

Gets the length of the string

string="abcd"
echo ${#string} #输出 4

Extract substrings

string="alibaba is a great company"
echo ${string:1:4} #输出liba

Find substrings

string="alibaba is a great company"
echo `expr index "$string" is`

Note: "'" in the script above is quotation marks, not single quotes "", don't get it wrong.


Shell array

bash supports one-dimensional arrays (multi-dimensional arrays are not supported) and does not limit the size of arrays.

Similar to the C language, the underling of the array element is numbered from 0. G ets the elements in the array to take advantage of the subsort, which can be an integer or arithmetic expression whose value should be greater than or equal to 0.

Define the array

In Shell, arrays are represented in parentheses, and array elements are split by the "space" symbol. T he general form of defining an array is:

数组名=(值1 值2 ... 值n)

For example:

array_name=(value0 value1 value2 value3)

Or

array_name=(
value0
value1
value2
value3
)

You can also define individual components of an array separately:

array_name[0]=value0
array_name[1]=value1
array_name[n]=valuen

Continuous synths can be used, and there is no limit to the scope of the underlying.

Read the array

The general format for reading array element values is:

${数组名[下标]}

For example:

valuen=${array_name[n]}

You can get all the elements in the array using the symbol, for example:

echo ${array_name[@]}

Gets the length of the array

The method for getting the length of an array is the same as the method for getting the length of a string, for example:

# 取得数组元素的个数
length=${#array_name[@]}
# 或者
length=${#array_name[*]}
# 取得数组单个元素的长度
lengthn=${#array_name[n]}

Shell comments

The line that begins with a comment is a comment that is ignored by the interpreter.

There are no more than one line of comments in sh, only one plus a sign on each line. C an only be like this:

#--------------------------------------------
# 这是一个自动打ipa的脚本,基于webfrogs的ipa-build书写:
# https://github.com/webfrogs/xcode_shell/blob/master/ipa-build
# 功能:自动为etao ios app打包,产出物为14个渠道的ipa包
# 特色:全自动打包,不需要输入任何参数
#--------------------------------------------
##### 用户配置区 开始 #####
#
#
# 项目根目录,推荐将此脚本放在项目的根目录,这里就不用改了
# 应用名,确保和Xcode里Product下的target_name.app名字一致
#
##### 用户配置区 结束  #####

What if, during development, you encounter a large piece of code that needs to be temporarily annotated and uncommented later?

Each line is too labory to add a symbol, you can put this piece of code to comment in a pair of braces, defined as a function, there is no place to call this function, this code will not be executed, and the same effect as the comment.