#TIL #bash 큰따옴표 안에 있는 물결표(Tilde)는 확장 안 된다

$ MD="~/My Documents"
$ echo "${MD}"
~/My Documents
$ cd "${MD}"
bash: cd: ~/My Documents: No such file or directory

물결표(~)는 HOME 환경 변수값으로 확장한다. 그런데, 확장이 안 된다. 이상하다.

$ MD=~/My\ Documents
$ echo "${MD}"
/c/Users/ohyecloudy/My Documents
$ cd "${MD}"
$ pwd
/c/Users/ohyecloudy/My Documents

큰따옴표(double quotes) 안에 있으면 확장 안 되는 것 아닐까? 큰따옴표를 쓰니 물결표 그대로 쓰이는 것 같다.

Enclosing characters in double quotes (‘”’) preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘`’, ‘\’, and, when history expansion is enabled, ‘!’. - 3.1.2.3 Double Quotes

아~ 확장되는 문자 중에 물결표는 없어요.

$ MD="$HOME/My Documents"
$ cd "${MD}"
$ pwd
/c/Users/ohyecloudy/My Documents

큰따옴표 안 쓰면 된다. 아니면 물결표 대신 $HOME 환경 변수값으로 확장해주면 된다.

참고

Feedback plz <3 @ohyecloudy, ohyecloudy@gmail.com

A Random Post