3 ms·
grep "echo ${data}" means that when grep is exec-ed, it receives these arguments: argv[0] = "grep" /* possibly full path, irrelevant here */ argv[1] = "
by kazinator 9y ago
grep "echo ${data}"
means that when grep is exec-ed, it receives these arguments:
argv[0] = "grep" /* possibly full path, irrelevant here */
argv[1] = "echo <contents of data variable>"
argv[2] = NULL
If this does what you want, it means that you wanted to look for strings starting with "echo ". All of arv[1] is the pattern to look for (because it doesn't look like a command line option). Since there are no other arguments, grep is scanning its standard input.
OTOH
grep `cat filelist.txt`
gets execed like this:
argv[0] = "grep"
argv[1] = "<first 'word' from filelist.txt>"
argv[2] = "<second 'word' from filelist.txt>"
...
argv[n] = "<last 'word' from filelist.txt>"
argv[n+1] = NULL
Needless to say, grep doesn't work this way; it doesn't accept multiple patterns as arguments. We cannot use "grep foo bar baz file" to scan file for occurrences of foo, bar or baz. Rather, what this means is to scan bar, baz and file for occurrences of foo.
Also, there is the risk that material from filelist.txt looks like command line options for grep. If filelist.txt contains "-v foo bar" then grep `cat filelist.txt` means exactly the same thing as "grep -v foo bar".
grep has a -f option which can specify a file whose lines are patterns:
grep -f filelist.txt
If we use the -F option, those patterns are interpreted as fixed strings, like the single pattern given on the command line.
- cup-of-tea 9y agofgrep is the way to go here. It uses a different algorithm that makes only one pass through the file while searching for all strings simultaneously. An extension of trie search.
- kazinator 9y agofgrep is an obsolete command that didn't make it into POSIX. I already mentioned the -F option of grep. "fgrep is the same as grep -F" -- GNU grep man page. I would expect that when the patterns in -f <patternfile> are treated as regular expressions, they are also combined into one regex.
- cup-of-tea 9y agoI know it's the same thing. I prefer the name fgrep because it does do a different thing. I write "grep -F" in scripts, but I say fgrep.
- quietbritishjim 9y agoI don't understand how this is an answer. If I follow your argument correctly, and totally ignoring for a moment what ${data} might evaluate to, then for the first command to produce any output it is necessary for the output of ls to at least contain the string "echo". But we can see that it does not, yet the claim (which I haven't tested) is that it does produce output. Why is that?
- kazinator 9y agoIt doesn't work, at least in a POSIX-like shell: 0:terada:~$ mkdir testdir 0:terada:~$ cd testdir 0:terada:~/testdir$ data="testfile1 testfile2 testfile3" 0:terada:~/testdir$ touch $data 0:terada:~/testdir$ ls -1 testfile1 testfile2 testfile3 0:terada:~/testdir$ ls | grep "echo ${data}" 1:terada:~/testdir$ I've seen this sort of shenanigan more than once in comp.unix.shell; usually followed by "oops, I somehow confused myself and can no longer reproduce this" type of backpedaling. The submitted web page doesn't have a properly presented repro test case; it's writing about something which allegedly happened on some machine somewhere, possibly with inaccuracies and omissions. We don't know what shell this is. Maybe the command that was actually run was ls | `echo ${data}`, data happened to be empty at the time, and the shell being used expanded that backtick expression to an empty argument instead of expanding to no argument. An empty regex matches everything.
- indigodaddy 9y agoThis was done on CentOS 7.3.x (not CentOS 6 as I had incorrectly first noted), and the following versions of bash and grep: "GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)" "grep (GNU grep) 2.20" So where $data is a newline delimited list of items/patterns: grep "${data}" ..will indeed match each item/pattern individually and grep over each, eg, just as grep -f does against a file with a list of patterns. It doesn't work without the double quotes, and putting the echo in front was obvious derp in hindsight. See my comment where I replied to uraza and demonstrated this.
- kazinator 9y ago