Timeline for Why do options in a quoted variable fail, but work when unquoted?
Current License: CC BY-SA 3.0
15 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Aug 28, 2017 at 10:47 | comment | added | Stéphane Chazelas |
@HTNW, that's an oversimplification that can end up being misleading. The tokenisation done by the shell is very different from the split+glob operator applied to expansions. cmd a b is very different from a='a b'; cmd $a. Parameter expansion is not some sort of macro expansion like it was in the Thompson shell or it is with C macros or shell aliases. Think for instance of cases like a='a; reboot' or a='$(uname)' or a='a\ b', or a='a:b' IFS=:.
|
|
| Aug 28, 2017 at 4:43 | history | edited | muru | CC BY-SA 3.0 |
edited title
|
| Aug 27, 2017 at 12:24 | vote | accept | z32a7ul | ||
| Aug 27, 2017 at 2:55 | comment | added | michael |
l̶i̶t̶t̶l̶e̶ ̶k̶n̶o̶w̶n̶ ̶b̶a̶s̶h̶ ̶f̶a̶c̶t̶ (edit: see this included in another answer already!) variable assignment doesn't require quotes; e.g., a=$b is fine, no need to use a="$b", unless the RHS is an expression; and even then, the form $(...) acts as quotes, e.g., this is fine: a=$(...), no need to do a="$(...)".
|
|
| Aug 27, 2017 at 2:04 | comment | added | HTNW |
> used by humans. It'd be annoying to manually define the boundaries between arguments, so shells split on whitespace to turn a line (a list of characters) into an argument vector (a list of lists of characters). Variable expansion is one the first expansions bash does, so you can imagine that $a is exactly equivalent to directly writing its contents. Now the issue is evident: a="-a -b"; cmd "$a" expands to cmd "-a -b", but cmd probably doesn't know what that means. cmd $a expands to cmd -a -b, which probably does work.
|
|
| Aug 27, 2017 at 1:56 | comment | added | HTNW |
I think it helps to understand how command line arguments work at a low level. When a program is executed, it receives arguments as a list of lists of characters (close enough). Each inner list is what we call an "argument." Most programs depend on logical separation between args. Here, you see that wget doesn't know what --mirror --no-host-directories means (as one argument), but it handles it when it's split into two arguments. Very few programs treat spaces and quotes specially once they are inside the argument vector. The problem is that bash, and other shells, are meant to be >
|
|
| Aug 27, 2017 at 0:55 | history | edited | Gilles 'SO- stop being evil' |
edited tags
|
|
| Aug 27, 2017 at 0:55 | answer | added | Gilles 'SO- stop being evil' | timeline score: 16 | |
| Aug 26, 2017 at 21:47 | history | tweeted | twitter.com/StackUnix/status/901561853419167745 | ||
| Aug 26, 2017 at 19:48 | comment | added | Scott - Слава Україні | Also discussed under Security implications of forgetting to quote a variable in bash/POSIX shells | |
| Aug 26, 2017 at 12:49 | comment | added | glenn jackman | Go to the source for the rules: the bash manual. Pay close attention to section 3.5 "Shell Expansions", especially word splitting and filename expansion -- these 2 factors are what you use quotes to control. | |
| Aug 26, 2017 at 12:49 | answer | added | Kusalananda♦ | timeline score: 28 | |
| Aug 26, 2017 at 12:47 | answer | added | glenn jackman | timeline score: 34 | |
| Aug 26, 2017 at 12:44 | comment | added | glenn jackman | Your question is answered here: mywiki.wooledge.org/BashFAQ/050 | |
| Aug 26, 2017 at 12:40 | history | asked | z32a7ul | CC BY-SA 3.0 |