简体   繁体   中英

Grep fails when file name stored in variable using BASH symbols like { and }

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM