# replace the first "dir" with "path" echo${str/dir/path} → /path1/dir2/my.file.txt
# replace all "dir" with "path" echo${str//dir/path} → /path1/path2/my.file.txt
Advanced
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
str="/dir1/dir2/my.file.txt"
# replace the head "dir" with "path" echo${str/#dir/path} → /dir1/dir2/my.file.txt # No change # reassign str str="dir0/dir1/dir2/my.file.txt" echo${str/#dir/path} → path0/dir1/dir2/my.file.txt
# replace the ending "dir" with "path" echo${str/%dir/path} → dir0/dir1/dir2/my.file.txt echo${str/%txt/conf} → dir0/dir1/dir2/my.file.conf