type

string

Strings are one of the most commonly used data types in Shell programming (there are no other types besides numbers and strings). Strings can be enclosed by single quotes "", double quotes "", or without quotes. There are differences between them, which we'll explain later.Copy the code
str1=value1

str2="value2"

str3='value3'
Copy the code

Three of the following

  • String surrounded by single quotation marks

    • Any string can be printed as is, and using variables in it is invalid

    • Single quotes cannot appear in strings, even if they are translated

  • A string surrounded by double quotation marks

    • If it contains a variable, the change is parsed (to get the value of the variable) rather than printed as is

    • Double quotes can appear in strings, as long as they are translated

  • A string that is not enclosed by quotes

    • Variables in unquoted strings are also parsed, just as in double-quoted strings

    • The string cannot contain Spaces. Otherwise, the string changed after Spaces will be parsed as other variables or commands

Gets the length of the string

# format
${#string_name}

# eg 
label="value"

echo ${#label}
Output # 5
Copy the code

String splicing

#! /bin/bash

foo="Hello"
bar="world"

# No Spaces in between
str1=$foo$bar

# If enclosed by double quotation marks, Spaces can be included
str2="$foo $bar"

Other strings can appear in the middle
str3=$foo":"$bar

# I can write it this way
str4="$foo: $bar"

The variable name needs to be enclosed in curly braces
str5="${foo}Script: ${bar}index.html"

Copy the code

String interception

This approach takes two parameters: in addition to specifying the starting position, you also need to truncate the length to finally determine which string to truncate.Copy the code

Counting starts from the left side of the string

${string: start :length}
Copy the code
  • stringIs the string to intercept
  • startIs the starting position (starting from the left and counting from 0)
  • lengthIs the length to be truncated.
url="www.github.com"

echo ${url: 5: 6}

# github
Copy the code

Count from the right side of the string

${string: 0-start :length}
Copy the code

Compared to the first format, the second format has only 0-, which is a fixed notation for counting from the right side of the string. Note:

  • When counting from the left, the starting number is 0 (which is programmer thinking); When you start counting from the right, you start with a number of 1 (this is normal thinking). The starting number is different depending on the counting direction.

  • No matter which side you start counting from, you intercept from left to right.

An array of

In the Shell, arrays are represented by parentheses (), and array elements are separated by Spaces. Thus, the general form of the array is defined asCopy the code
# define array
arr=(ele1 ele2 ele3 ele4)
Copy the code

Note that assignment = cannot have Spaces on either side and must be next to the array name and element. Shells are weakly typed and do not require that all array elements be of the same type.

nums=(20 40 "https://github.com")
Copy the code

The third element is an “anomaly”, where the first two elements are integers and the third element is a string.

Shell arrays are not fixed in length, and you can add elements after definition. For example, for the arR array above, which has a length of 4, use the following code to add an element at the end, extending its length to 5:

arr[4]=ele5
Copy the code

Getting an array element

Display the element of the specified index of the array
${arr[index]}

Display the first element of the array
${arr[0]}
Copy the code

Where arr is the array name and index is the subscript. We use @ and * to get all the elements in the array:

Display all elements of array
${arr[*]}
${arr[@]}

# display the number of elements in the array
${#arr[@]}
Copy the code

An array of joining together

The idea behind concatenating arrays is to expand them into lists using @ or *, and then join them together. The specific format is as follows

array_new=(${array1[@]}  ${array2[@]})
array_new=(${array1[*]}  ${array2[*]})
Copy the code

The two methods are equivalent. Just choose one of them. Array1 and array2 are the arrays to be spliced, and array_new is the new array to be spliced.

Deleting an array element

unset array_name[index]
Copy the code

Array_name indicates the array name and index indicates the array subscript. If you don’t write the subscript, you write it like this:

unset array_name
Copy the code

So you delete the entire array, and everything disappears.