Search code examples
rubyescapingcommand-substitution

How to pass a ruby script's output as arguments to a shell command using command substitution?


I have the following in a_script.rb:

if ARGV.empty?
    puts "string_without_spaces 'string with spaces'"
else
    p ARGV
end

When I run:

ruby a_script.rb `ruby a_script.rb`

I get the following output:

["string_without_spaces", "'string", "with", "spaces'"]

But instead, I would like to have the following output:

["string_without_spaces", "string with spaces"]

How do I change the script to get the wanted output?


Solution

  • The problem is in your bash command. Backtick is escaping the " and '

    You can use xargs

    ruby a_script.rb | xargs ruby a_script.rb