I've noticed a lot of questions and answers and comments expressing disdain for (and sometimes even fear of) writing scripts instead of one-liners. So, I'd like to know:
When and why should I write a stand-alone script rather than a "one-liner"? Or vice-versa?
What are the use-cases and pros & cons of both?
Are some languages (e.g. awk or perl) better suited to one-liners than others (e.g. python)? If so, why?
Is it just a matter of personal preference or are there good (i.e. objective) reasons to write one or the other in particular circumstances? What are those reasons?
one-liner: any sequence of commands typed or pasted directly into a shell command-line. Often involving pipelines and/or use of languages such as
perl, and/or tools like
It is the direct execution on the command-line that is the defining characteristic - the length and formatting is irrelevant. A "one-liner" may be all on one line, or it may have multiple lines (e.g. sh for loop, or embedded awk or sed code, with line-feeds and indentation to improve readability).
script: any sequence of commands in any interpreted language(s) which are saved into a file, and then executed. A script may be written entirely in one language, or it may be a shell-script wrapper around multiple "one-liners" using other languages.
I have my own answer (which I'll post later), but I want this to become a canonical Q&A on the subject, not just my personal opinion.