0

How can I make calls to the command line using the back tick and variables? Something like:

myvar = "C:\Program Files"
`cd ` + myvar
Mischa
  • 42,876
  • 8
  • 99
  • 111
MGreenfield
  • 355
  • 1
  • 13

2 Answers2

4

Also, consider using a system() call, for clarity. Backticks are for short commands.

system allows for a visually-more-obvious open + close block formatting that befits large, or multi-line OS instructions.

See this SO Q+A


Though, if you're writing large OS scripts, put them in a shell file, check it into VCS, and exec that with a ruby one-liner.

Community
  • 1
  • 1
New Alexandria
  • 6,951
  • 4
  • 57
  • 77
  • Strange answer. You suggest `system`, but the accepted answer in that link says you should use backticks instead of `system`. – Mischa Sep 06 '13 at 21:36
  • @Mischa read it closer; it does not say to use backticks '*instead*' – it says that you *don't need to* call `system`. Which is true. But system allows for a *visually-more-obvious* open + close block formatting that befits large, or multi-line OS instructions. – New Alexandria Sep 06 '13 at 21:43
  • I agree with your point that it's visually more obvious, but that link does not seem to be supporting or expanding on what you say. Although the topic is similar, it seems unrelated to this question. – Mischa Sep 07 '13 at 01:34
3

Try this:

`cd "#{myvar}"`

Example:

$ irb --simple-prompt
>> `pwd`
=> "/home/kirti\n"
>> var = 'ruby'
=> "ruby"
>> `cd "#{var}" && pwd`
=> "/home/kirti/ruby\n"
Mischa
  • 42,876
  • 8
  • 99
  • 111
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317