-1

I have to implement a minishell written in C for my school homework,and I am currently working on the parsing of the user input and I have a question regarding single quote and double quote.

How are they parsed, I mean many combo are possible as long as there is more than two single quotes / double quote. Let’s say I have : ""hello"".

If I echo this the bash output would be hello, is this because bash interpreted "hello" inside "" or because bash interpreted this as : "" then hello "". How does bash parse multiple chained quotes : " "Hello" " : let’s say a = Hello. Does bash sees "a" or bash see ""a""

dieri
  • 23
  • 6
  • This might help: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus Jan 01 '23 at 09:24
  • All of the information about quoting in bash is available at your fingertips (assuming a standard bash install). Just type `man bash` to get the documentation, and then `/^quo` to skip down to the section on quoting. (`/` does a regular expression search; ^ anchors the search at the start of the line, good for searching for section headers in long documents.) – rici Jan 01 '23 at 20:49

1 Answers1

1

Bash parses single- and double-quotes slightly differently. Single-quotes are simpler, so I'll cover them first.

A single-quoted string (or single-quoted section of a string -- I'll get to that) runs from a single-quote to the next single-quote. Anything other than a single-quote (including double-quotes, backslashes, newlines, etc) is just a literal character in the string. But the next single-quote ends the single-quoted section. There is no way to put a single-quote inside a single-quoted string, because the next single-quote will end the single-quoted section.

You can have differently-quoted sections within a single "word" (or string, or whatever you want to call it). So, for example, ''hello'' will be parsed as a zero-length single-quoted section, the unquoted section hello, then another zero-length single-quoted section. Since there's no whitespace between them, they're all treated as part of the same word (and since the single-quoted sections are zero-length, they have no effect at all on the resulting word/string).

Double-quotes are slightly different, in that some characters within them retain their special meanings. For example, $ can introduce variable or command substitution, etc (although the result won't be subject to word-splitting like it would be without the double-quotes). Backslashes also function as escapes inside double-quotes, so \$ will be treated as a literal dollar-sign, not as the start of a variable expansion or anything. Other characters that can have their special meaning removed by backslash-escaping are backslashes themselves, and... double-quotes! You can include a double-quote in a double-quote by escaping it. The double-quoted section ends a the next non-escaped double-quote.

So compare:

echo ""hello""    # just prints hello
echo "\"hello\""  # prints "hello", because the escaped
                  # quotes are part of the string
echo "$PATH"      # prints the value of the PATH variable
echo "\$PATH"     # prints $PATH
echo ""'$PATH'""  # prints $PATH, because it's in
                  # single-quotes (with zero-lenght
                  # double-quoted sections on each side

Also, single- and double-quotes have no special meaning within the other type of quote. So:

echo "'hello'"    # prints 'hello', because the single-quotes
                  # are just ordinary characters in a
                  # double-quoted string
echo '"hello"'    # similarly, prints "hello"
echo "'$PATH'"    # prints the PATH variable with
                  # single-quotes around it (because
                  # $variable expands in double-quotes)
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
  • Thanks for the answee so as single quotes the next encoutered double quote means the end of the first double quote ? – dieri Jan 01 '23 at 09:54
  • @dieri Almost. In a double-quoted section, an escaped double-quote doesn't end the double-quoted section, but the next double-quote *that isn't escaped* does end it. So if bash sees `"foo\"bar...` it considers that the double-quoted section is still going, and `bar` and everything after it is double-quoted; but if it sees `"foo"bar...` then the double-quoted section ended after `foo`, `bar` and whatever's after it is unquoted, and if if sees another quote (of either type) that'll start a completely new quoted section. – Gordon Davisson Jan 01 '23 at 10:11