CDToad
CDToad

Reputation: 41

How to wrap bash command substitution output in quotes

Trying to pull strings out of files found with bash's Parenthesis () - Command substitution. I am using this command

strings $(locate '.tps' |egrep -i customer | egrep -i billing)

And it works fine with files that have no spaces. When a file has a space this happens

strings: '/sales/rep/company/October': No such file
strings: 'bill': No such file
strings: 'and': No such file
strings: 'invoice': No such file
strings: 'customer': No such file
strings: 'name.tps': No such file

If I try wrap the command in double quotes

strings "$(locate '.tps' |egrep -i customer | egrep -i billing)"

It concatenates all the files that it finds into one BIG filename

Upvotes: 0

Views: 241

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295363

Doing this in bash with GNU tools might look like:

locate '.tps' | egrep -i customer | egrep -i billing | xargs -d $'\n' strings --

...or:

locate '.tps' | egrep -i customer | egrep -i billing | while IFS= read -r file; do
  strings "$file"
done

That said, it's not safe to use newline-delimited streams to represent lists of filenames at all, because files on common UNIX filesystems (including ext3, ext4, btrfs, etc) are permitted to contain newlines themselves. With GNU locate:

locate -0 '*.tps' | egrep -i --null-data '(customer.*billing|billing.*customer)' | xargs -0 strings --

Upvotes: 3

Related Questions