# Backquotes question

**URL:** <https://www.ruby-forum.com/t/backquotes-question/117160>\
**Category:** Ruby\
**Created:** [October 14, 2007, 2:30am UTC](https://www.ruby-forum.com/t/backquotes-question/117160 "2007-10-14T02:30:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 14, 2007, 2:30am UTC](https://www.ruby-forum.com/t/backquotes-question/117160/1 "2007-10-14T02:30:55Z")

</div>

Hello, I would like to know why  
cmd = “/bin/df -H /dev/#{partition}”  
used = `#{cmd}`

works fine but this fails  
used = `"/bin/df -H /dev/#{partition}"`

with an OS error.

Thank you

BTW, is there an aliase to the backquote ? I mean a kernel method doing  
the same - Just for the syntax, backquotes are not very sexy.

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 14, 2007, 2:45am UTC](https://www.ruby-forum.com/t/backquotes-question/117160/2 "2007-10-14T02:45:31Z")

</div>

Zouplaz wrote:

> BTW, is there an aliase to the backquote ? I mean a kernel method doing  
> the same - Just for the syntax, backquotes are not very sexy.

You don’t need the quotes when you surround the command with backquotes.  
Use this instead:

used = `/bin/df -H /dev/#{partition}`

When you use both quotes and backquotes, the quotes are treated as part  
of the command. That’s what causes the error.

You can use %x instead of backquotes:

used = %x(/bin/df -H /dev/#{partition})

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 14, 2007, 2:58am UTC](https://www.ruby-forum.com/t/backquotes-question/117160/3 "2007-10-14T02:58:16Z")

</div>

Zouplaz wrote:

> Hello, I would like to know why  
> cmd = “/bin/df -H /dev/#{partition}”  
> used = `#{cmd}`
> 
> works fine but this fails  
> used = `"/bin/df -H /dev/#{partition}"`
> 
> with an OS error.

Well, first of all they aren’t equivalent. For instance, writing this:

str = “hello”  
puts “Hi, #{str} world.”

isn’t equivalent to writing:

puts “Hi, “hello” world.”

I don’t know enough about Unix to know the reason for the system error,  
but this works:

flags = “-l”  
puts `ls #{flags}`

But, this does not:

puts `"ls -l"`

The internal quotes messes things up.

> BTW, is there an aliase to the backquote ? I mean a kernel method doing  
> the same - Just for the syntax, backquotes are not very sexy.

puts %x{ls -l}

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 14, 2007, 3:40am UTC](https://www.ruby-forum.com/t/backquotes-question/117160/4 "2007-10-14T03:40:30Z")

</div>

On Oct 13, 8:28 pm, Zouplaz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> BTW, is there an aliase to the backquote ? I mean a kernel method doing  
> the same - Just for the syntax, backquotes are not very sexy.

I think the syntax is great, but you can also use the following:

puts Kernel::`(“ls”)

Still uses a backquote, but it’s more like a traditional method call.

Or as described on p. 351 of the pickaxe, you can alias the backquote:

alias :cmd :`  
puts cmd “ls”

This worked for me when running a program, but not from irb.

But if you give it some time, I think you may like the simplicity of:

result = `ls`

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 16, 2007, 2:02pm UTC](https://www.ruby-forum.com/t/backquotes-question/117160/5 "2007-10-16T14:02:13Z")

</div>

le 14/10/2007 05:21, Gerardo S. Gómez Garrido nous a dit:

> “/bin/df -H /dev/wd0a”
> 
> and the shell cannot find the program named “/bin/df -H /dev/wd0a”

Thanks for the hint (and others posters too) !

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 14, 2007, 5:22am UTC](https://www.ruby-forum.com/t/backquotes-question/117160/6 "2007-10-14T05:22:13Z")

</div>

2007/10/13, Zouplaz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hello, I would like to know why  
> cmd = “/bin/df -H /dev/#{partition}”  
> used = `#{cmd}`
> 
> works fine but this fails  
> used = `"/bin/df -H /dev/#{partition}"`
> 
> with an OS error.

Let partition be “wd0a”.

In the first case, you’re executing:

/bin/df -H /dev/wd0a

which the shell interprets correctly as a call the program /bin/df  
with the specified options and parameters.

In the second case you’re executing:

“/bin/df -H /dev/wd0a”

and the shell cannot find the program named “/bin/df -H /dev/wd0a”
