#TIL #bash 셸 파라미터 확장 - 공백을 포함한 path

$ cd My\ Documents
$ pwd
/c/Users/ohyecloudy/My Documents

parameter에 공백이 있는 경로를 할당하고 cd 명령어 인자로 넘기려면?

$ MD=My\ Documents
$ cd $MD
bash: cd: too many arguments
$ cd "$MD"
$ pwd
/c/Users/ohyecloudy/My Documents

path가 공백을 포함하려면 큰따옴표(double quotes)로 감싸야 한다.

$ MD="My Documents"
$ cd $MD
bash: cd: too many arguments
$ cd "$MD"

이건 또 왜? 이미 큰따옴표로 감쌌잖아. 그냥 써도 될 것 같은데. 셸 파라미터 확장(shell parameter expansions) 후에 뭔가 일어나는 것 같다.

After the preceding expansions, all unquoted occurrences of the characters ‘\’, ‘’’, and ‘”’ that did not result from one of the above expansions are removed. - 3.5.9 Quote Removal

맞네. \, ‘, ” 문자는 제거된다. 그래서 큰따옴표로 감싸야 한다.

참고

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

A Random Post