Do take care: leaving out the quotes can lead not just to errors but to security holes. Unless you mean for all this rigmarole to happen, just remember to always use double quotes around variable and command substitutions. See When is double-quoting necessary? for more details about the cases when you can leave out the quotes. The output of arithmetic substitution also undergoes the same expansions, but that isn't normally a concern as it only contains non-expandable characters (assuming IFS doesn't contain digits or -). On a side note, don't use `foo`: its quoting rules are weird and non-portable, and all modern shells support $(foo) which is absolutely equivalent except for having intuitive quoting rules. See What is the difference between $* and same happens to command substitution with $(foo) or with `foo`. is equivalent to "$1" "$2" "$3" if there are three positional parameters. (Exception: to expand to the list of positional parameters, e.g. Double quotes delimit a string context: the whole double-quoted string is a single string, not to be split. Field splitting and filename generation only happen in list context, but that's most of the time. There are two contexts in shell syntax: list context and string context. Note that the result is a list of strings. If the current directory is empty, the result is foo, *, bar. In our example, this results in the list containing foo, following by the list of files in the current directory, and finally bar. If the pattern doesn't match any files, it is left unmodified. as a wildcard pattern, and replace it by the list of file names that match this pattern. Filename generation: treat each field as a glob, i.e.For example, if the variable contains foo * bar then the result of this step is the 3-element list foo, *, bar. Field splitting: treat that value as a whitespace-separated list of fields, and build the resulting list.$foo does not mean “take the value of the variable foo”. Why do I need to write "$foo"? What happens without the quotes? Note that the explanations below contains a few approximations (statements that are true in most conditions but can be affected by the surrounding context or by configuration). If you want the whole nitty-gritty, read the standard or your shell's manual. Zsh users should skip it and read the end of When is double-quoting necessary? instead. This answer applies to Bourne/POSIX-style shells ( sh, ash, dash, bash, ksh, mksh, yash…). Xargs treats whitespace and the characters \"' specially. Instead of find … | xargs, prefer find … -exec …. If you must use xargs, make that xargs -0. Plain read treats backslashes and whitespace specially. read - To read input line by line with the read builtin, use while IFS= read -r line do ….If you use $foo unquoted, your script will choke on input or parameters (or command output, with $(foo)) containing whitespace or \[*?. Always use double quotes around variable substitutions and command substitutions: "$foo", "$(foo)"
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |