Home > Back-end >  Grep fails when file name stored in variable using BASH symbols like { and }
Grep fails when file name stored in variable using BASH symbols like { and }

Time:11-18

I have two text files identical to each other a.text and b.text with the same content.

Content of a.text and b.text

abcd
target
efgh

Can anyone explain why one of the commands work but not the other and if there is a way of making it work?

Output of command 1

grep "target" {a,b}.text
>>a.text:target
b.text:target

Output of command 2

file="{a,b}.text"
grep "target" $file
>>grep: {a,b}.text: No such file or directory

Happy if someone can point me to a location where I can read more about this as well. I can only assume that when storing it as a variable it explicitly looks for a file called {a,b}.text although, what I am not as sure about is why and what leads to that.

CodePudding user response:

As user1934428 said, brace expansion happens before parameter expansion. Quoting from the bash manual:

Expansion is performed on the command line after it has been split into words. There are seven kinds of expansion performed: brace expansion, tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, word splitting, and pathname expansion.

The order of expansions is: brace expansion, tilde expansion, parameter, variable and arithmetic expansion and command substitution (done in a left-to-right fashion), word splitting, and pathname expansion.

To get this to work you can store the file names in an array. Arrays can hold multiple file names without being subject to quoting or expansion issues that plague normal string variables.

files=({a,b}.text)
grep "target" "${files[@]}"

This works because {a,b} is now evaluated when the variable is assigned, rather than when it is expanded.

CodePudding user response:

In the first case, you have a brace-expansion. In the second case, you are searching a file with the literal name {a,b}.text. The reason is that in bash, brace expansion happens before parameter expansion.

  • Related