http://blog.csdn.net/shuanghujushi/article/details/51298672
During the bash Shell's use, there are a number of string manipulations that are often encountered, and some of the individual usage summaries are summarized below.
First, the definition of a string
You can define a string with double quotes, single quotes, or directly after =, as defined by three strings
str1="this is a string" str2=‘this is a string‘str3=this_is
But double and single quotes are still different in bash.
single quote , the literal meaning of all characters of the string is preserved, and the single quotation marks are not allowed to appear in single quotes, and escape characters are not escaped.
double quotation marks , some extensions are supported, such as $ (dollar character) \ (backslash). \$ can refer to the following variable values, \ (backslash) to indicate escape. As follows:
str="test_sting."echo "\\$str" #输出结果为:\test_stringecho ‘\\$str‘ #输出结果为:\\$str
Second, to find the length of the string
str="test_string"echo ${#str} #得到长度9
Three, string comparison
The string in the bash shell can be performed, greater than, less than, equal to, judged whether it is empty, whether there are values, and so on. In an equal operation, regular matching is supported. The specific syntax is as follows:
operator |
meaning |
Example |
< |
Less than |
[[ "1" < "2" ]] #true ASCII 字母顺序 |
> |
Greater than |
[[ "2" > "1" ]] #true ASCII 字母顺序 |
== |
Equals (supports pattern matching) |
[[ "aa" == "aa" ]] #true |
|
|
[[ "aaa" == a* ]] #true 模式匹配 Cannot add double quotes when pattern matching |
= |
equal to = = = equivalent |
|
=~ |
Judgment string |
[[ "aa" =~ "a" ]] #true |
!= |
Not equal to |
[[ "a" != "b" ]] #true |
-Z |
is empty |
[[ -z ""]] #true |
-N |
is not empty |
[[ -n "a" ]] #true |
When comparing strings in a bash shell, it is generally best to use "(double quotation marks) to enclose the values to avoid the effect of whitespace." However, you cannot use double quotes when using d**== (pattern matching) * *
Iv. truncation of strings
You can use symbols such as #,##,%,%% to truncate a string. The bash shell string supports four truncation methods, either truncating the left character or truncating the right character.
Suppose there is a string str
str="This-is-a-test-string"
operator |
meaning |
Example |
# |
# Intercept, intercept the shortest match from the left, and keep the right character. |
echo ${str#*-} #得到is-a-test-string |
## |
# #号截取, capture the longest match from the left and keep the right character |
echo ${str##*-} #得到string |
% |
% intercept, capturing the shortest match from the right, preserving the left character |
echo ${str%-*} #得到This-is-a-test |
%% |
Intercept the longest match from the right, leaving the left character |
echo ${str%%-*} #得到This |
V. Interception of strings
The
Shell string supports sub-string interception at any location. The syntax is:
${org_string:pos:length} intercepts the length of a substring starting at POS
Span style= "Color:coral" >${org_string:pos:-length} intercepts the substring that starts at the POS and intercepts the length from the end of the string
${org_string: (-pos): length} starts at the POS position ending at the end of the string and intercepts the length of the substring
${org_string: (-pos):-length} from the POS position ending at the end of the string, intercept the substring at the position of length long from the end of the string;
Span style= "Color:coral" >${org_string:pos} intercept start from Pos to end of string
${org_ String: (-pos)} starts at the POS position ending at the end of the string and intercepts the end of the string
Use the following:
org_str= " This-is-a-test-test-string "echo ${org_str:0:4} #得到 thisecho ${org_str:0:-4} #得到 this-is-a-test-test-stecho ${ ORG_STR: ( -11): 4} #得到 testecho ${ORG_STR: ( -11): -4} #得到 testecho ${org_str:4} #得到-is-a-test-test-stringecho ${org_str ( -6)} #得到 string
Vi. Substitution of strings
In the bash shell string, you can replace substrings in the old string with a new string.
Suppose there is a string org_str
org_str="This-is-a-test-test-string"
Specific usage, such as the following table:
An expression |
meaning |
Example |
${org_str/Sub_str/replace_str} |
Replace the first matching $sub_str with the value of $REPLACE_STR |
${org_str/test/TEST} #得到This-is-a-TEST-test-string |
${org_str//Sub_str/replace_str} |
Replace all matching $sub_str with the value of $REPLACE_STR |
${org_str//test/TEST} #得到This-is-a-TEST-TEST-string |
${org_str/#Sub_str/replace_str} |
If $ORG_STR is starting with $SUB_STR, replace $sub_str with the value of $REPLACE_STR |
${org_str/#This/THIS} #得到THIS-is-a-test-test-string |
${org_str/%Sub_str/replace_str} |
If $org_str ends with $sub_str, the value of $REPLACE_STR is used to replace the $SUB_STR |
Linux shell Learning (string manipulation)--01