0

I was completing the git tutorial found here: https://try.github.io/levels/1/challenges/7

And it said that I had to put single quotes around *.txt. I had not seen this before when using linux but thought it was peculiar. I also have seen single quotes when using html and php as a way to make sure that the string is interpretted literally instead of using special characters.

2 Answers 2

7

This is the same in the shell as in the other grammars that you mention. A single quoted string will be treated as a "string literal" (so to speak).

The difference between git add '*.txt' and git add *.txt is who's doing the matching of the pattern against filenames.

In the case of git add '*.txt', git is doing the pattern matching. Since the shell won't expand the string literal '*.txt', git add will be called with a single argument, *.txt. git then does the matching against the filenames available in the whole repository (because... git).

In the case of git add *.txt, the shell does the filename matching and will pass a list of matching filenames from the current directory to git add. Note that if there are no names matching the given pattern, the shell will (usually1) pass the pattern on to git add unexpanded. If this happens, the result will be the same as if the pattern had been quoted.

1 Usually, but see e.g. the failglob shell option in bash. See also comments to this answer.


When git add gets a filename pattern, it will add not only the files matching in the current directory, but it will add all files that matches in the whole repository (i.e. including any subdirectories). This is why the text in the lower right hand corner says

Wildcards:

We need quotes so that Git will receive the wildcard before our shell can interfere with it. Without quotes our shell will only execute the wildcard search within the current directory. Git will receive the list of files the shell found instead of the wildcard and it will not be able to add the files inside of the octofamily directory.

2
  • 2
    It's worth noting that when the shell does the filename matching and matches nothing, git add will get *.txt as if you run git add '*.txt'. If this is what you want you may think you do it right until one time there is a matching file in the current directory, the whole behavior changes and you don't know what hit you. Aug 28, 2017 at 7:32
  • 2
    @Kamil, only in shells where that hasn't been fixed. See zsh/fish/tcsh for a better behaviour (and the failglob option in bash). Another issue is that if there's a file called *.txt in the currently directory, an unquoted *.txt will expand to that and the other .txt files, and git would do another round of different wildcard interpretation. Aug 28, 2017 at 10:58
1

Single quoting preventing glob and variable expansion by shell but double quoting prevent only globbing not variable expansion.

*.txt will expand by shell and will result listing of all .txt files. but quoting '*.txt' will prevent it. see below examples.

echo *.txt
a.txt b.txt c.txt

echo '*.txt'
*.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .