Username Username - 6 months ago 12
Ruby Question

How do I loop through elements of a bash array variable passed to a Ruby script via terminal?

I have a Bash file called

test.sh
:

#!/bin/bash
arr=(whatever@gmail.com iamcool@gmail.com)
ruby testScript.rb $arr


And here's
testScript.rb
:

ARGV.each{|a| p a}


When I run
./test.sh
in the terminal, I get this output:

"whatever@gmail.com"


I want my Ruby script to loop through all elements in the Bash array variable
$arr
. How do I fix my code to do this?

Answer

In the bash script, pass the array like this:

ruby testScript.rb ${arr[@]}
Comments