Bash Shell string operations summary

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Load from:

1 the length of

str="abcd"
expr length $str   # 4
echo ${#str}       # 4
expr "$str" : ".*" # 4
Like the general use of second

2 substring search position

str="abc"
expr index $str "a"  # 1
expr index $str "b"  # 2
expr index $str "x"  # 0
expr index $str ""   # 0 

3 select substring

str="abcdef"
expr substr "$str" 1 3  # Take 3 characters from the first position,  abc
expr substr "$str" 2 5  # Take 5 characters from the second position,  bcdef 
expr substr "$str" 4 5  # Take 5 characters from the fourth position,  def

echo ${str:2}           # Extraction of string from the second position,  bcdef
echo ${str:2:3}         # The extraction of 3 characters from the second position, bcd
echo ${str:(-6):5}        # Extraction of string from the last second position to the left, abcde
echo ${str:(-4):3}      # The extraction of 6 characters from the second position to the left, cde


4 intercepted substring 

str="abbc,def,ghi,abcjkl"
echo ${str#a*c}     # The output, def, GHI, abcjkl a well number (#) said off the shortest match from the left truncation (here abbc string remove)
echo ${str##a*c}    # Output JKL, two well number (##) said off the longest match from the left truncation (here is the abbc, def, GHI, ABC strings removed)
echo ${str#"a*c"}   # The output of abbc, def, GHI, abcjkl because STR not "a*c" substring
echo ${str##"a*c"}  # The output of abbc, def, GHI, abcjkl similarly
echo ${str#*a*c*}   # Empty
echo ${str##*a*c*}  # Empty
echo ${str#d*f)     # Output abbc,def,ghi,abcjkl, 
echo ${str#*d*f}    # Output,ghi,abcjkl   

echo ${str%a*l}     # abbc,def,GHI a percent sign (%) said from the right to intercept the shortest match 
echo ${str%%b*l}    # A two percent (%) said that the longest match from the right to intercept
echo ${str%a*c}     # abbc,def,ghi,abcjkl  

 Can this memory, well number (#) are often used to represent a number, it is placed in front of a percent sign (%); rear unloading digital; or so the memory, on a keyboard layout, well number (#) is located in the percent (%) on the left (the front)   :-)

The 5 string replacement 

str="apple, tree, apple tree"
echo ${str/apple/APPLE}   # Replace the first occurrence of apple
echo ${str//apple/APPLE}  # Replace all apple

echo ${str/#apple/APPLE}  # If the string STR to Apple at the beginning, then replace it with the APPLE
echo ${str/%apple/APPLE}  # If the string STR ends with apple, then replace it with the APPLE


6 Comparison

[[ "a.txt" == a* ]]        # Logical truth(pattern matching)
[[ "a.txt" =~ .*\.txt ]]   # Logical truth(regex matching)
[[ "abc" == "abc" ]]       # Logical truth(string comparision) 
[[ "11" <"2" ]]           # Logical truth (string comparision), according to the ASCII value
 

7 connection 

s1="hello"
s2="world"
echo ${s1}${s2}   # Of course, wrote $s1$s2 also, but the best with braces


The 8 turning

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Margaret at November 11, 2013 - 8:18 AM