Linux ${} variable content extraction and replacement functions, etc.

Source: Internet
Author: User

[[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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.