Linux Shell series (5) Shell string, linuxshell
This article is part 5 of the Linux Shell series tutorials. For more shell tutorials, see the Linux Shell series tutorials.
String is the most commonly used and useful data type in Shell programming. Today, Linux Emy-NET introduces the usage of string in Shell and the string operations commonly used in Shell.
Shell string usage
Strings in Shell can be enclosed in quotation marks or not.
You can use double quotation marks or single quotation marks. The difference between single double quotes is similar to that in PHP.
Next, we will introduce the differences and usage examples of single string double quotation marks in Shell.
Single quotes
Usage example
str='www.linuxdaxue.com'
Features of single quotes:
- Any character in Shell single quotes will be output as is, and the variable in single quotes string is invalid;
- Single quotes cannot appear in Shell single quotes (cannot use escape characters for single quotes ).
Double quotation marks
Usage example
myweb='linuxdaxue.com'str="Hello, you are browsing \"$myweb\"! \n"
Advantages of double quotation marks:
- Variable in Shell double quotation marks
- Shell double quotes can contain escape characters
Therefore, it is recommended that you add quotation marks to the string when using Shell, and it is best to add double quotation marks.
Shell string operations
The following table lists the operations on strings in Shell:
| Expression |
Description |
| $ {# String} |
$ String Length |
| $ {String: position} |
In $ string, the substring is extracted from position $ position. |
| $ {String: position: length} |
In $ string, the Child string with the length of $ length is extracted from the position $ position. |
| $ {String # substring} |
From the beginning of the variable $ string, delete the substring that matches the minimum $ substring. |
| $ {String ## substring} |
Removes the substring that matches $ substring at the beginning of the variable $ string. |
| $ {String % substring} |
Removes the substring that matches the minimum value of $ substring from the end of the variable $ string. |
| $ {String % substring} |
Removes the substrings that match the longest $ substring from the end of the variable $ string. |
| $ {String/substring/replacement} |
Use $ replacement to replace the first matched $ substring. |
| $ {String // substring/replacement} |
Use $ replacement to replace all matched $ substring |
| $ {String/# substring/replacement} |
If the $ string prefix matches $ substring, $ replacement is used to replace the matched $ substring. |
| $ {String/% substring/replacement} |
If the $ string suffix matches $ substring, $ replacement is used to replace the matched $ substring. |
Examples of common Linux Shell operations
Next we will give you some examples of common operations.
1) Output String Length
[linuxdaxue @linuxdaxue ~]$ test='I love china'[linuxdaxue @linuxdaxue ~]$ echo ${#test}12
$ {# Variable name} returns the string length
2) truncate a string
[linuxdaxue @linuxdaxue ~]$ test='I love china'[linuxdaxue @linuxdaxue ~]$ echo ${test:5} e china[linuxdaxue @linuxdaxue ~]$ echo ${test:5:10}e china
$ {Variable name: Start: length} to obtain the substring
3) delete a string
[linuxdaxue @linuxdaxue ~]$ test='c:/windows/boot.ini'[linuxdaxue @linuxdaxue ~]$ echo ${test#/}c:/windows/boot.ini[linuxdaxue @linuxdaxue ~]$ echo ${test#*/}windows/boot.ini[linuxdaxue @linuxdaxue ~]$ echo ${test##*/}boot.ini[linuxdaxue @linuxdaxue ~]$ echo ${test%/*}c:/windows[linuxdaxue @linuxdaxue ~]$ echo ${test%%/*}
$ {Variable name # substring Regular Expression} starts with a string and starts with a substring to delete matching expressions.
$ {Variable name % substring Regular Expression} starts from the end of the string with substring to delete the matching expression.
Note: $ {test # */} and $ {test %/*} are the simplest methods to get the file name or directory address.
4) string replacement
[linuxdaxue@linuxdaxue~]$ test='c:/windows/boot.ini'[linuxdaxue@linuxdaxue~]$ echo ${test/\//\\}c:\windows/boot.ini[linuxdaxue@linuxdaxue~]$ echo ${test//\//\\}c:\windows\boot.ini
$ {Variable/search/replace value} A "/" indicates replacing the first, "//" indicates replacing all. When the search appears: "/" Please add an escape character.
Well, the above are some basic concepts and Common commands of Shell strings. I hope you can master them well.
For more shell tutorials, see the Linux Shell series tutorials.
Fixed Link: linux Emy network-Linux Shell series tutorial (5) Shell string