5 ms·
This only works accidentally. If the output of ls is not a terminal, then it is single-filename-per-line (and sorted alphabetically): $ ls nomatchfile1 no
by ploxiln 8y ago
This only works accidentally.
If the output of ls is not a terminal, then it is single-filename-per-line (and sorted alphabetically):
$ ls
nomatchfile1 nomatchfile2 testfile1 testfile10 testfile2 testfile3 testfile4
$ ls | cat
nomatchfile1
nomatchfile2
testfile1
testfile10
testfile2
testfile3
testfile4
The first argument to grep is the pattern, the following arguments to grep are the files to search. If there are any files specified, stdin is not read/searched.
$ grep testfile1 testfile2 testfile3
$ ls | grep testfile1 testfile2 testfile3
Quoting affects whether you pass one or multiple arguments to grep:
$ echo ${data}
testfile1 testfile2
$ echo "$data"
testfile1
testfile2
Finally, newlines in the pattern cause grep to treat each line as a separate pattern:
$ ls | grep "$data"
testfile1
testfile10
testfile2
Backticks alone do not quote their contents, so the expanded backticks output is treated as multiple arguments.
etc. etc. lots of ways to mix this all up :)
- caf 8y agoThis also means that "echo testfile1" is one the patterns passed to grep here.
- ploxiln 8y agoyup. which is why "testfile1" was not in the result of the original experiment: $ ls | grep "echo ${data}" testfile10 testfile2 ...
- throwaway2048 8y agodo not parse the output of ls http://mywiki.wooledge.org/ParsingLs http://mywiki.wooledge.org/ParsingLs
- indigodaddy 8y agoExcellent link, ty. More things that I should not have been doing... :)