Rubyコマンドにbash変数を挿入するには?

Rubyコマンドにbash変数を挿入するには?

私はbashシェルを使用しています。次の変数を定義しました...

localhost:tmp davea$ echo $json
{"id": "abc", "name": "dave"}

このvarの値をRuby命令に挿入したいので、これを試しました...

localhost:tmp davea$ ruby -rjson -e 'j = JSON.parse($json); puts j["Instances"][0]["ImageId"]'
Ignoring bindex-0.5.0 because its extensions are not built.  Try: gem pristine bindex --version 0.5.0
Ignoring byebug-11.0.1 because its extensions are not built.  Try: gem pristine byebug --version 11.0.1
Ignoring byebug-10.0.2 because its extensions are not built.  Try: gem pristine byebug --version 10.0.2
Ignoring byebug-10.0.1 because its extensions are not built.  Try: gem pristine byebug --version 10.0.1
Ignoring byebug-10.0.0 because its extensions are not built.  Try: gem pristine byebug --version 10.0.0
Ignoring childprocess-1.0.1 because its extensions are not built.  Try: gem pristine childprocess --version 1.0.1
Ignoring debase-0.2.4 because its extensions are not built.  Try: gem pristine debase --version 0.2.4
Ignoring eventmachine-1.2.5 because its extensions are not built.  Try: gem pristine eventmachine --version 1.2.5
Ignoring ffi-1.11.1 because its extensions are not built.  Try: gem pristine ffi --version 1.11.1
Ignoring ffi-1.9.25 because its extensions are not built.  Try: gem pristine ffi --version 1.9.25
Ignoring ffi-1.9.23 because its extensions are not built.  Try: gem pristine ffi --version 1.9.23
Ignoring ffi-1.9.18 because its extensions are not built.  Try: gem pristine ffi --version 1.9.18
Ignoring hiredis-0.6.1 because its extensions are not built.  Try: gem pristine hiredis --version 0.6.1
Ignoring json-2.1.0 because its extensions are not built.  Try: gem pristine json --version 2.1.0
Ignoring nio4r-2.4.0 because its extensions are not built.  Try: gem pristine nio4r --version 2.4.0
Ignoring nio4r-2.3.1 because its extensions are not built.  Try: gem pristine nio4r --version 2.3.1
Ignoring nio4r-2.3.0 because its extensions are not built.  Try: gem pristine nio4r --version 2.3.0
Ignoring nio4r-2.2.0 because its extensions are not built.  Try: gem pristine nio4r --version 2.2.0
Ignoring nokogiri-1.10.4 because its extensions are not built.  Try: gem pristine nokogiri --version 1.10.4
Ignoring nokogiri-1.8.2 because its extensions are not built.  Try: gem pristine nokogiri --version 1.8.2
Ignoring nokogiri-1.8.1 because its extensions are not built.  Try: gem pristine nokogiri --version 1.8.1
Ignoring pg-1.1.4 because its extensions are not built.  Try: gem pristine pg --version 1.1.4
Ignoring pg-0.21.0 because its extensions are not built.  Try: gem pristine pg --version 0.21.0
Ignoring puma-3.12.1 because its extensions are not built.  Try: gem pristine puma --version 3.12.1
Ignoring puma-3.11.4 because its extensions are not built.  Try: gem pristine puma --version 3.11.4
Ignoring puma-3.11.3 because its extensions are not built.  Try: gem pristine puma --version 3.11.3
Ignoring puma-3.11.2 because its extensions are not built.  Try: gem pristine puma --version 3.11.2
Ignoring rmagick-2.16.0 because its extensions are not built.  Try: gem pristine rmagick --version 2.16.0
Ignoring ruby-debug-ide-0.7.0 because its extensions are not built.  Try: gem pristine ruby-debug-ide --version 0.7.0
Ignoring ruby-filemagic-0.7.2 because its extensions are not built.  Try: gem pristine ruby-filemagic --version 0.7.2
Ignoring sqlite3-1.4.1 because its extensions are not built.  Try: gem pristine sqlite3 --version 1.4.1
Ignoring sqlite3-1.3.13 because its extensions are not built.  Try: gem pristine sqlite3 --version 1.3.13
Ignoring websocket-driver-0.6.5 because its extensions are not built.  Try: gem pristine websocket-driver --version 0.6.5
Ignoring websocket-native-1.0.0 because its extensions are not built.  Try: gem pristine websocket-native --version 1.0.0
/Users/davea/.rvm/gems/ruby-2.4.0/gems/json-2.1.0/lib/json/pure/parser.rb:135:in `convert_encoding': nil is not like a string (TypeError)
    from /Users/davea/.rvm/gems/ruby-2.4.0/gems/json-2.1.0/lib/json/pure/parser.rb:78:in `initialize'
    from /Users/davea/.rvm/gems/ruby-2.4.0/gems/json-2.1.0/lib/json/common.rb:156:in `new'
    from /Users/davea/.rvm/gems/ruby-2.4.0/gems/json-2.1.0/lib/json/common.rb:156:in `parse'
    from -e:1:in `<main>'

私の$ jsonが翻訳されていないようです。上記のコマンドに挿入する正しい方法は何ですか?

答え1

一重引用符を使用します。 Bashでは、一重引用符内の変数は拡張されません。

ruby -rjson -e "j = JSON.parse($json); puts j[\"Instances\"][0][\"ImageId\"]"

関連情報