0

Recently, I am busy coding the bash script to feed my needs. However, during the coding time, I found the usage of double quotes is contradicted with each other at some times. For example, I try to find some files that matched the regex, such as ls "*.log". I habitually wrap the *.log with double quotes but get the error cannot access '*.txt': No such file or directory.

After that, I found the reason from a tutorial that it is useless to put the * in double-quotes when you want the * acts as a wildcard. Therefore, ls *.log is the correct usage.

However, when I use the grep command with regex, all three of them ("",'',non-quote) do not change the original meaning of my regex, such as grep "^s.txt". At this time, double-quotes make the regex active.

Is there exist some design conflicts between the commands? And could you please give me some tricks to quickly grasp the correct usage of quotes for usual commands?

  • "Is there exist some design conflicts between the commands?" yes, of course; these are completely two different commands and having different functionalities. see man grep and man ls for more info. – αғsнιη Jul 03 '20 at 03:38
  • Shells such as bash use globbing to represent multiple filenames using special characters called wildcards with a single filename. A wildcard is essentially a symbol which may be used to substitute for one or more characters. bash also supports extended globbing; The options extglob and globstar must be set to get the extended functionality, i.e. shopt -s globstar. – fpmurphy Jul 03 '20 at 03:47
  • @αғsнιη, I mean..I think grep and ls are all builtin bash commands at the very beginning, so they shouldn't conflict the rule of bash that how to handle and parse the double-quotes. However, now I found grep is not the builtin command. – Simon Hu Jul 04 '20 at 11:40

1 Answers1

2

In case of ls *.log it's the shell who expands the wildcard before ls is started. Then ls is given the result. The tool has no functionality to expand * by itself.

In case of grep "^s.txt" the shell only removes the quotes (which are still useful in general to tell the shell how to treat the quoted string), grep gets ^s.txt and does its job. In case of unquoted ^s.txt the shell has nothing to do, so the result is the same. But it could be different in general (e.g. with a*.*txt).

Compare this: find and globbing (and wildcards). Both the find -name and the shell are capable of using * as a wildcard. It depends on the quoting which one will act. The results are usually different, sometimes erroneous. You need to know whether you want the shell to expand * or not.