I am not getting results back from grep when I call it with a param containing curly braces using Ruby’s “ or %x{} expressions.
Say I have these files in an empty directory:
$ echo '**hello**' > hello.md
$ echo 'hello' > hello.txt
If I only want results matching a single extension, Ruby works as expected:
$ grep -rn hello . --include=*.txt
./hello.txt:1:hello
$ grep -rn hello . --include=*.md
./hello.md:1:**hello**
$ irb
ree-1.8.7-2011.03 :001 > `grep -rn hello . --include=*.txt`
=> "./hello.txt:1:hello\n"
ree-1.8.7-2011.03 :002 > %x{grep -rn hello . --include=*.md}
=> "./hello.md:1:**hello**\n"
ree-1.8.7-2011.03 :003 > exit
But if I ask for results matching either extension, using grep’s curly brace syntax, no results come up with Ruby:
$ grep -rn hello . --include=*.{txt,md}
./hello.txt:1:hello
./hello.md:1:**hello**
$ irb
ree-1.8.7-2011.03 :001 > `grep -rn hello . --include=*.{txt,md}`
=> ""
ree-1.8.7-2011.03 :002 > %x{grep -rn hello . --include=*.{txt,md}}
=> ""
Edit: echoing works, but command results are still empty:
ruby-1.9.2-p180 :004 > `echo grep -rn hello . --include=*.{txt,md}`
=> "grep -rn hello . --include=*.{txt,md}\n"
ruby-1.9.2-p180 :005 > `grep -rn hello . --include=*.{txt,md}`
=> ""
How can I debug this and see the actual command being executed? And what’s the correct way to execute system commands with curly braces in the params from Ruby?
Here’s a guess:
%x{}is using/bin/shwhich isn’t behaving like/bin/bash(or your usual shell).This pattern:
is expanded by your shell just like all the other file globbing patterns. When you run your command from within
irb, Ruby is probably using/bin/shto execute the command and on your system,/bin/shbehaves like a plain POSIX shell which doesn’t understand the*.{a,b}globbing syntax.Try invoking
bashdirectly: