💾 Archived View for jacksonchen666.com › posts › 2024-02-25 › 00-15-39 › index.gmi captured on 2024-03-21 at 14:48:45. Gemini links have been rewritten to link to archived content

View Raw

More Information

➡️ Next capture (2024-06-16)

-=-=-=-=-=-=-

Confusing Shell Quotes

2024-02-25 00:15:39Z

A command

Here's a command. It will be run in the bash shell. What do you think this command will output?

for i in wh"y is" this a" thin'"''; do echo $i; done

All you have to worry about is how `wh"y is" this a" thin'"''` will be split up by the shell. The rest of the command is just print each thing onto a new line, and what's considered "another argument" is up to the shell.

If you just got absolutely confused, don't worry, here's the output:

why is
this
a thin'

Confusion 100

Here's another command. What will this output?

echo \
''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""\
""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''\
''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""\
""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''\
''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""\
""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''\
''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""\
""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''\
''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""\
""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''\
''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""\
""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''\
''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""\
""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''\
''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""\
""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''\
''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""\
""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''\
''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""\
""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''\
''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""\
""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''\
''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""\
""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''\
''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""\
""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''\
''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""\
""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''""''

Well it's actually just the same as running echo. It'll give you an empty line.

That's because every quote here and new line as well has been reduced to nothing. There actually no contents in any of those quotes.

More realistic example: Literal apostrophe within a quote using apostrophes

Those were just some ridiculous examples. Here's some more realistic examples that you may or may not need someday.

Let's say you're using single quotes to quote, and then you have to use a single quote within a single quote. Here's what it would look like for the sentence "Woody's wood":

echo 'Woody'\''s wood'

Here's the purpose of every apostrophe character in order:

1. Start quoting stuff (So far: `echo '`)

2. After the text "Woody", the quote is closed, returning back to the usual shell interpretation. (So far: `echo 'Woody'`)

3. An escaped apostrophe for you know, an apostrophe. (So far: `echo 'Woody'\'`)

4. Start quoting stuff again. (So far: `echo 'Woody'\''`)

5. After the text "s wood", the quote is closed. (So far: `echo 'Woody'\''s wood'`)

So the apostrophe quoting is closed, returning back to the shell interpretation of stuff, then an escaped apostrophe is used, and then the apostrophe quoting returns.

A more reasonable solution would be to use double quotes, but there are cases where you just have to use single quotes (apostrophes) because double quotes won't work.

public inbox (comments and discussions)

public inbox archives

(mailing list etiquette for public inbox)