
Ravi Iyer
A shell script is a program in a language with several syntaxes and no diagnostics, so the quoting is not optional. A variable holding a filename with a space expands into two words unless it is quoted, and the usual fix is quoting everything, every time. Command substitution has the same problem, and so does a variable holding text you did not write. The word splitting rules differ between shells and between settings, which is why a script behaves differently under a different shell or with a different profile loaded. POSIX sh is the portable subset and bash adds arrays, brace expansion, pattern matching operators and a test construct that handles patterns and arithmetic. That test construct is worth its own paragraph. The older form takes a pattern and has its own escaping rules, and a pattern containing a bracket silently fails to match. The newer form behaves as expected, and mixing the two is the usual cause of a test that works in an interactive shell and fails in a script. The scheduling difference is the one that catches people. A cron job gets a near-empty environment, so a command that relies on an interactive setting fails with no obvious reason, and it fails silently because the output goes to a mailbox nobody reads. I finish on set and the options that change a script's behaviour for its children, since those propagate in ways that surprise. Every page here includes the form that fails, because the failure is what teaches the rule and the rule alone does not.
About ToolSura
ToolSura offers 80+ free, privacy-first online tools that run 100% in your browser — no uploads, no logins. Learn more about our mission →