# Backquote and variables

**URL:** <https://www.ruby-forum.com/t/backquote-and-variables/73464>\
**Category:** Ruby\
**Created:** [October 24, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/backquote-and-variables/73464 "2006-10-24T20:17:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rlorimor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@rlorimor](https://www.ruby-forum.com/u/rlorimor)\
**Post date:** [October 24, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/backquote-and-variables/73464/1 "2006-10-24T20:17:03Z")

</div>

I am trying to construct a command line to be executed using backquote  
`` notation. i.e. capturing the output to a string

e.g.

#!/usr/bin/ruby -w  
file = “/etc/motd”  
string = `"cat " + file`  
puts string

% ruby test.rb  
sh: line 1: cat : command not found

This doesn’t work. It appears that everything between the backquotes is  
treated as a quoted string. i.e. no variable substitution takes place.

How can I work around this?

---

<div class="post-metadata">

**Author:** ![rlorimor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@rlorimor](https://www.ruby-forum.com/u/rlorimor)\
**Post date:** [October 24, 2006, 8:25pm UTC](https://www.ruby-forum.com/t/backquote-and-variables/73464/2 "2006-10-24T20:25:03Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I am trying to construct a command line to be executed using backquote `` notation. i.e. capturing the output to a string
> 
> e.g.
> 
> #!/usr/bin/ruby -w  
> file = “/etc/motd”  
> string = `"cat " + file`

string = `#{"cat "} + file`

---

<div class="post-metadata">

**Author:** ![rlorimor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@rlorimor](https://www.ruby-forum.com/u/rlorimor)\
**Post date:** [October 24, 2006, 8:29pm UTC](https://www.ruby-forum.com/t/backquote-and-variables/73464/3 "2006-10-24T20:29:33Z")

</div>

On 10/24/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> sh: line 1: cat : command not found
> 
> This doesn’t work. It appears that everything between the backquotes is treated as a quoted string. i.e. no variable substitution takes place.
> 
> How can I work around this?

The backtick string is treated like a double-quote. Therefore, you can  
do this:  
file = ‘/etc/motd’  
motd = `cat #{file}`  
puts motd

---

<div class="post-metadata">

**Author:** ![rlorimor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@rlorimor](https://www.ruby-forum.com/u/rlorimor)\
**Post date:** [October 24, 2006, 8:36pm UTC](https://www.ruby-forum.com/t/backquote-and-variables/73464/4 "2006-10-24T20:36:59Z")

</div>

Joel VanderWerf wrote:

> string = `#{"cat "} + file`

Oops, I didn’t really read your example. I assumed you were trying to  
get the command name interpolated in the string. Wilson answered your  
question.

If you wanted to interpolate both, you could do this:

cmd=“cat”  
file="/etc/motd"  
motd = `#{cmd} #{file}`
