MGreenfield
MGreenfield

Reputation: 365

Set var to system call output and suppress console display

I need to set a variable to the output of a command line call but not display that information to the console. For instance

output = `echo asdf`

But not actually display "asdf" or "echo asdf" to the console. Is that possible?

Upvotes: 0

Views: 157

Answers (2)

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84443

Return Nil in REPL

When using irb or pry, the last expression on a line is the only expression printed to the console by the REPL loop. For example:

output = %x(echo asdf); nil
# => nil

You can use this to suppress output to the console, but you aren't really adding any real security since (by definition) any variable available inside the REPL is accessible to the person at the console. Thus:

output = %x(echo foo); nil
# => nil
output
# => "foo\n"

Return Nothing with Pry

With pry, you can disable output altogether by making a semicolon the last item in a line. For example:

[1] pry(main)> output = `echo asdf`;
[2] pry(main)> 

This works with pry, but not with irb. Your mileage may vary with this technique.

Upvotes: 1

Arup Rakshit
Arup Rakshit

Reputation: 118299

Start your IRB as below :

kirti@kirti-Aspire-5733Z:~$ irb --noecho
2.0.0p0 :001 > output = `echo asdf`
2.0.0p0 :002 > output
2.0.0p0 :003 > puts output
asdf
2.0.0p0 :004 > 

Upvotes: 0

Related Questions