3 ms·
You can technically omit the double-quotes during a scalar variable assignment (as long as it's not preceded by a modifier such as `export`), so the outermost q
by js2 3y ago
You can technically omit the double-quotes during a scalar variable assignment (as long as it's not preceded by a modifier such as `export`), so the outermost quotes are not needed above.
https://unix.stackexchange.com/questions/68694/when-is-double-quoting-necessary https://unix.stackexchange.com/questions/68694/when-is-doubl...
The extra quotes don't hurt, but also: use shellcheck. It will tell you if you've omitted necessary quotes.
- lloeki 3y ago> The extra quotes don't hurt I have the habit of quoting everything, that way I don't have to think about any corner case where I can skip quotes, and thus conversely never miss anything where I should quote. I do the same with curly braces, that way I don't have to think whether "${foo_bar}" or "${foo}_bar" was meant, and makes later adding or reading string manipulation (e.g "${haystack/needle/replacement}") or arrays (e.g "${foo[42]}" or "${foo[@]}" that much more consistent: variable references are always bounded by ${} which makes code easier to scan. I apply that to "${1}" and "${@}" too whenever possible, unfortunately bash 3.x doesn't quite like that so it's not always the case (I can't decide which one I hate more: macOS for the refusal to either update bash or outright drop it† and just have a plain posix sh + zsh, or GNU for the viral license move which is highly detrimental in practice for that case). I also often quote "semantically", e.g: # instead of: PATH="/some/path/${foo}/bar:${PATH}" /some/command "--foo=${bar}" # I'd rather do: PATH="/some/path/${foo}/bar":"${PATH}" /some/command --foo="${bar}" † I guess there's a crapton of bash scripts that have been written and possibly embedded in apps or installers, so backwards compatibility won here.
- stouset 3y agoAre you me? :)
- stouset 3y agoI actually personally use `declare` frequently, particularly to make variables readonly. So in my case the quotes are necessary. Of course this has its own issues, in that subprocess exit codes are hidden, so you often need to assign then declare. But I quote everything, always, because then I don’t have to rely on remembering when it’s necessary and when it isn’t. Just do the safe thing every time and you don’t have to think.