Recommanded Free YOUTUBE Lecture: <% selectedImage[1] %>

문자열 길이

  • ${#string}
  • expr length

문자열 바꾸기

vim의 문자열 치환에 익숙하다면 비교적 쉽게 사용할 수 있다.

${string/substring/replacement} 문자열 string에서 첫번째로 일치하는 substring를 찾아서 replacement문자열로 바꾼다. ${string//substring/replacement} 문자열 strng에서 substring과 일치하는 모든 문자열을 찾아서 replacement문자열로 바꾼다.
stringZ=abcABC123ABCabc

echo ${stringZ/abc/xyz}       # xyzABC123ABCabc
                              # Replaces first match of 'abc' with 'xyz'.

echo ${stringZ//abc/xyz}      # xyzABC123ABCxyz
                              # Replaces all matches of 'abc' with # 'xyz'.

echo  ---------------
echo "$stringZ"               # abcABC123ABCabc
echo  ---------------
                              # The string itself is not altered!

# Can the match and replacement strings be parameterized?
match=abc
repl=000
echo ${stringZ/$match/$repl}  # 000ABC123ABCabc
#              ^      ^         ^^^
echo ${stringZ//$match/$repl} # 000ABC123ABC000
# Yes!          ^      ^        ^^^         ^^^

echo

# What happens if no $replacement string is supplied?
echo ${stringZ/abc}           # ABC123ABCabc
echo ${stringZ//abc}          # ABC123ABC
# A simple deletion takes place.

${string/#substring/replacement} 문자열 string의 처음에 일치하는 substring을 replacement 문자열로로 변환한다.

${string/%substring/replacement} 문자열 string의 마지막에 일치하는 substring을 replacement 문자열로로 변환한다.