Linux Shell Scripting Tutorial Series (5) Shell String

This article is the fifth part of the Linux Shell Scripting Series . For more shell tutorials, please see: Linux Shell Scripting Series Tutorials

Strings are the most commonly used and useful data types in Shell programming. Following the previous article , today, Linux University.com will introduce the usage of strings in Shell and the commonly used string operations in Shell.

 

Shell string usage

Strings in the shell can be enclosed in quotes or not.

If you use quotation marks, you can use double quotation marks or single quotation marks. The difference between single and double quotes is similar to PHP.

Next, I will introduce the difference and usage examples of single and double quotation marks in strings in Shell.

 

add single quotes

Usage example

str='justcode.ikeepstudying.com'

 

Features of adding single quotes:

  • Any character in shell single quotes will be output as it is, and variables in single-quoted strings are invalid;
  • Single quotes cannot appear in shell single-quoted strings (nor does using escape characters for single quotes).

 

add double quotes

 

Usage example

myweb='justcode.ikeepstudying.com'
str="Hello, you are browsing \"$myweb\"! \n"

 

Advantages of adding double quotes:

  • Shell double quotes can have variables
  • Escape characters can appear in shell double quotes

Therefore, it is recommended that you add quotation marks to strings when using Shell, and it is best to add double quotation marks.

 

Manipulating Shell Strings

The operations on strings in Shell are shown in the following table:

Expression meaning
${#string} length of $string
${string:position} In $string, extract the substring starting at position $position
${string:position:length} In $string, extract a substring of length $length starting at position $position
${string#substring} From the beginning of the variable $string, delete the shortest substring matching $substring
${string##substring} From the beginning of the variable $string, delete the longest substring matching $substring
${string%substring} From the end of the variable $string, delete the shortest substring matching $substring
${string%%substring} Remove the longest substring matching $substring from the end of the variable $string
${string/substring/replacement} Use $replacement, to replace the first matching $substring
${string//substring/replacement} Use $replacement, to replace all matching $substrings
${string/#substring/replacement} If the prefix of $string matches $substring, then use $replacement to replace the matched $substring
${string/%substring/replacement} If the suffix of $string matches $substring, then use $replacement to replace the matching $substring

 

Examples of common operations in Linux Shell

Here are some examples of common operations

 

1) Output string length

[justcode@ikeepstudying ~]$ test='I love china'
[justcode@ikeepstudying ~]$ echo ${#test}
12

 ${#variable name} get the length of the string

 

2) Intercept the string

[justcode@ikeepstudying ~]$ test='I love china'
[justcode@ikeepstudying ~]$ echo ${test:5}    
e china
[justcode@ikeepstudying ~]$ echo ${test:5:10}
e china

 ${variable name:start:length} get substring

 

3) Deletion of strings

[justcode@ikeepstudying ~]$ test='c:/windows/boot.ini'
[justcode@ikeepstudying ~]$ echo ${test#/}
c:/windows/boot.ini
[justcode@ikeepstudying ~]$ echo ${test#*/}
windows/boot.ini
[justcode@ikeepstudying ~]$ echo ${test##*/}
boot.ini

[justcode@ikeepstudying ~]$ echo ${test%/*}
c:/windows
[justcode@ikeepstudying ~]$ echo ${test%%/*}

 ${variable name#substring regular expression} is equipped with substring from the beginning of the string, and the matching expression is deleted.

${variable name%substring regular expression} is equipped with substring from the end of the string, and the matching expression is deleted.

Note: ${test##*/}, ${test%/*} are the easiest ways to get the file name or directory address respectively.

 

4) String replacement

[justcode@ikeepstudying~]$ test='c:/windows/boot.ini'
[justcode@ikeepstudying~]$ echo ${test/\//\\}
c:\windows/boot.ini
[justcode@ikeepstudying~]$ echo ${test//\//\\}
c:\windows\boot.ini

 ${variable/find/replace value} A "/" means to replace the first one, "//" means to replace all, when "/" appears in the search, please add the escape character "\/" to indicate.

Well, the above are some basic concepts and common commands of Shell strings, I hope everyone can master them.

For more shell tutorials, please see: Linux Shell Scripting Series Tutorials

 

Original: Linux Shell Series Tutorial (5) Shell String

This article is transferred from: Linux Shell Scripting Tutorial Series (5) Shell String

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326595783&siteId=291194637