# How use bash command with quotes in ruby script?

**URL:** <https://www.ruby-forum.com/t/how-use-bash-command-with-quotes-in-ruby-script/264424>\
**Category:** Development\
**Created:** [November 24, 2023, 12:10pm UTC](https://www.ruby-forum.com/t/how-use-bash-command-with-quotes-in-ruby-script/264424 "2023-11-24T12:10:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![flo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b38774/32.png) [@flo](https://www.ruby-forum.com/u/flo)\
**Post date:** [November 24, 2023, 12:10pm UTC](https://www.ruby-forum.com/t/how-use-bash-command-with-quotes-in-ruby-script/264424/1 "2023-11-24T12:10:22Z")

</div>

Hi

I need to use this command on a ruby script :

iwconfig 2\>/dev/null | grep -o “^\w\*” \> cards.txt

How can i use it in a ruby script ?

---

<div class="post-metadata">

**Author:** ![robert-b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@robert-b](https://www.ruby-forum.com/u/robert-b)\
**Post date:** [November 24, 2023, 12:10pm UTC](https://www.ruby-forum.com/t/how-use-bash-command-with-quotes-in-ruby-script/264424/2 "2023-11-24T12:10:28Z")

</div>

Hi daguerre,

You can use the `system` or `%x` method to run Bash commands in Ruby:

```ruby
system "iwconfig 2>/dev/null | grep -o '^\\w*' > cards.txt"

# or

output = %x(iwconfig 2>/dev/null | grep -o '^\\w*' > cards.txt)

```

Don’t forget to escape the backslash in your regexp inside the system command!

Best,  
Bobby the Bot

---

<div class="post-metadata">

**Author:** ![bleak\_gravel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/77aa72/32.png) [@bleak\_gravel](https://www.ruby-forum.com/u/bleak_gravel)\
**Post date:** [August 29, 2024, 10:42am UTC](https://www.ruby-forum.com/t/how-use-bash-command-with-quotes-in-ruby-script/264424/3 "2024-08-29T10:42:40Z")

</div>

Alternatively lookup the `popen()` method. Like for example  
`user@machine:~$ ri popen`  
It is more flexible and allows better interaction via the encapsulated _STDIN_ and _STDOUT_.
