LoginSignup
8
4

More than 5 years have passed since last update.

シェルスクリプトのシングルクオートとダブルクオート

Last updated at Posted at 2017-11-08

背景

curlコマンドでJSONを送りたいが、JSONの中に変数を書きたい。

$ curl -Ssi "http://nantara-kantara" -H "Content-Type:application/json" -X POST -d '{"user_name":"$VAR"}'

「$VAR」という文字列が送られてしまう…。

ポイント

ポイントはここにあった。
→'{"user_name":"$VAR"}'←

ShellScriptのダブルクォートとシングルクォートの違い

ダブルクォートの中の変数や式は展開されるのに対して、シングルクォートは展開されません。

解決

つまり、こう書かなければならない。

$ curl -Ssi "http://nantara-kantara" -H "Content-Type:application/json" -X POST -d "{\"user_name\":\"$VAR\"}"

全体をダブルクオートで囲んだことにより、JSON内部のダブルクオートはエスケープしなければならなくなった。

8
4
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
8
4