LoginSignup
1
2

More than 5 years have passed since last update.

zsh でオプション指定を含む変数を埋め込みコマンドを実行する

Posted at

スペースやオプション指定を含む文字列をコマンドに展開して使おうとするとエラーとなる。

$ MYOPTIONS="-alh -G"
$ ls $MYOPTIONS
ls: illegal option --...

スペースを含む文字列として扱われてしまう。
そんなときは eval を使う。コマンドの先頭につけるだけ。

$ eval ls $MYOPTIONS
total 384
drwxr-xr-x   19 hiro  staff   646B 11  4 12:43 .
drwxr-xr-x   46 hiro  staff   1.5K 11  5 00:09 ..
-rw-r--r--    1 hiro  staff    34B 10 26 22:05 .babelrc
...

引数を複数含む場合も。

$ export ENABLEHOSTS="$(cat ./enable_hosts.txt |perl -pe 's/^/ -d /g'| perl -pe 's/\n//g')"

$ echo $ENABLEHOSTS
-d elzup.com -d www.elzup.com -d app.elzup.com ...

$ eval ./letsencrypt-auto certonly --standalone $ENABLEHOSTS
... Congratulations! ...
1
2
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
1
2