[[email protected] log]# var=/dir1/dir2/file.txt1, variable value [[email protected] log]# Echo ${var}/dir1/dir2/ File.txt2 the length of the string [[email protected] log]# echo ${#var}193, extract position 3 to the last character [[email protected] log]# Echo ${var:3 }R1/DIR2/FILE.TXT4, starting from position 3, start extracting 5 characters [[email protected] log]# Echo ${var:3:5}r1/di5, starting from the left, deleting the shortest match R, where the * means delete [[email protected] log]# Echo ${var#*r}1/dir2/file.txt6, start from left, delete shortest match dir[[email protected] log]# Echo ${var#*dir}1 /dir2/file.txt7, starting from the left, deleting the shortest match number, supporting regular expressions [[email protected] log]# Echo ${var#*[0-9]}/dir2/file.txt8, starting from the left, Delete the longest match di, where the * means delete [[email protected] log]# Echo ${var##*di}r2/file.txt9, start from the right, delete the shortest match di, note: Here's * in the back. #在 $ to the left, starting from the left,% on the right, starting from the right [[email protected] log]# Echo ${VAR%DI*}/DIR1/10, starting from the right, deleting the shortest match di[[email Protected] log]# echo ${VAR%%DI*}/11, replace the first matching di[[email protected with a AA] log]# echo ${var/di/aa}/aar1/dir2/ FILE.TXT12, replace all matching dir[[email protected with BBB] log]# Echo ${var//dir/bbb}/bbb1/bbb2/file.txt13, replace the prefix with BBB/dir, note :/required by itselfUsing the escape character [[email protected] log]# Echo ${var/#\/dir/bbb}bbb1/dir2/file.txt14, replacing the prefix with BBB le, note:/itself requires the use of escape characters [[email protected] log]# echo ${var/%\le*.txt/bbb}/dir1/dir2/fibbb15, replace suffix 2*.txt with BBB, support regular expression [[email protected] log]# Echo ${var/%2*.txt/bbb}/dir1/dirbbb
Linux ${} variable content extraction and substitution functions, etc.