Pedro Sturmer
Pedro Sturmer

Reputation: 604

Ruby only executing the first line?

I'm writing a ruby script and found this strange behaviour.

Using ruby 2.4.2 [x86_64-darwin16]

Basically I'm trying to echo two separated messages and in my index.rb file I got:

exec("echo 'teste'")
exec("echo 'teste2'")

But when I run ruby ./index.rb

The output is:

teste

Why that's happening?

Shouldn't this be the output?

testeteste2

Upvotes: 4

Views: 198

Answers (1)

mrzasa
mrzasa

Reputation: 23347

exec([env,] command... [,options])

Replaces the current process by running the given external command docs

It means that the first call to exec replaces your ruby program with echo, so the rest of the ruby program is not executed.

You can use backticks to run a command like you want:

`echo 'teste'`
`echo 'teste2'`

Upvotes: 7

Related Questions