12
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

bash と zshの変数展開の違い

Posted at

bash

word splitting に書いてあるとおり、double quotedされてない限り展開された後、tokenに分解されます:

bash-3.2$ abc='abc def ghi'
bash-3.2$ printf '%s\n' $abc
abc
def
ghi
bash-3.2$ printf '%s\n' "$abc"
abc def ghi

zsh

word splittingしてくれない。= をつける必要あり。

zsh$ abc='abc def ghi'
zsh$ printf '%s\n' $abc
abc def ghi
zsh$ printf '%s\n' "$abc"
abc def ghi
zsh$ printf '%s\n' $=abc 
abc
def
ghi
zsh$ printf '%s\n' "$=abc"
abc
def
ghi

おまけ

こういう、展開後の tokenの操作を展開される個々の要素でやるのって汚いよなあ、と思いつつ意地悪な実験:

zsh$ printf '%s\n' "${=abc}d"
abc
def
ghid
zsh$ printf '%s\n' "${=abc}d${abc}"
abc
def
ghidabc def ghi

文字列の該当する部分が =から展開されたかどうかで結果が変わってくるんだな。面倒臭そげ。S式に逃げたい。

12
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?