Shell Script First Repository. A collection of shell script examples, and a wiki detailing some commands and command-line topics.
We welcome your participation; raise an issue, or submit a pull request.
The example shell scripts are split into several directories:
⇪ POSIX Compliance
We pay particular attention to the POSIX IEEE Std 1003.1-2017 specification (Shell & Utilities). Unless explicitly stated, scripts and example extracts are POSIX-compliant. Such scripts will have the following shebang (or hash-bang) line:
#/usr/bin/env sh
Scripts that are intended to illustrate shell-specific features, will have that particular shell's name in place of the sh
.
Writing POSIX-compliant scripts increases their portability across Linux distributions and Unix variants. However, if your target is Linux and you’re using shells like bash, zsh or ksh (Korn Shell), you may choose to take advantage of their useful and convenient extensions rather than limiting yourself to POSIX compliance.
⇪ POSIX-Compliant Features
feature | description |
---|---|
$(‹command›) |
Command substitution (alternative: `‹command›` ). |
(‹command›) |
Run command(s) in subshell. |
$((‹expr›)) |
Arithmetic expansion. |
${‹var›#‹pattern›} |
Prefix removal. Also ## (for longest). |
${‹var›%‹pattern›} |
Postfix removal. Also %% (for longest). |
[‹args›] |
Tests. Alias for test . |
set -e |
Error handling. |
trap |
Signal handling. |
case |
Pattern matching. End with esac . |
shift |
Remove (pop) argument. |
for , while , until |
Iteration statements. End with done . |
if … elif … fi |
If statement. |
; , && , || |
Command sequencing. |
| , < , > , >> |
Piping and redirection. |
command |
Bypass aliases. |
exec |
Replace process or duplicate file descriptors. |
${‹var›:=‹default›} |
Set default value for unset variables. |
export |
Make variables available to sub-processes. |
return |
Exit code for functions. |
exit |
Exit code for process or script. |
getopts |
Short option parsing. |
IFS |
Field splitting delimiters. |
HOME |
User's home directory. |
PATH |
Executable directory list. |
LC… |
Locale variables. |
<< ‹mark> |
Here documents. |
<< '‹mark>' |
Here documents without expansions. |
⇪ Shell Extensions
feature | description |
---|---|
<(‹command›)) |
Process substitution. |
((‹expr›)) |
Arithmetic evaluation. |
[[‹expr›]] |
Extended test command. |
${‹var›/‹patt›/‹replace›} |
Subtitution. |
for((‹init›;‹cond›;‹augm›)) |
C-style for loop. |
local |
Local variables in functions. |
readonly |
Create constants. |
getopt |
Short & long option parsing. |
<< $‹var› |
Use variable content for marks. |
<<- ‹mark› |
Strip one leading ‹TAB›. |
<<< ‹string› |
Bash ‘here strings’. |
mktemp |
Reliable temporary filenames. |
$'…' |
ANSI-C quoting. |
echo -e |
Parse C escape sequences. |
echo -n |
Omit automatic newline. |
⇪ Some Resources
A non-exhaustive list of curated shell scripting and general command-line online resources.
-
Bash Reference Manual — The source of truth (for Bash, at least).
-
Advanced Bash-Scripting Guide (2014) — Still useful; also available as a PDF file.
-
The Linux Command Line -5ed (2019); William Shotts — Available as a free PDF, or as a printed version from No Starch Press. See home page for links.
-
ShellCheck — Online version of a shell script ‘linter’ (downloadable from GitHub, or via your package manager).
-
Shell Scripting Tutorial — A free online Bourne shell tutorial, with optional PDF purchase.
-
Effective Shell — Covers bash command-line and light shell scripting.
-
Online Help Resources — Online man pages and documentation resources list on this repository's wiki.
-
Unix Shell Genealogy — The ancestry of Unix-like shells stretches as far back as 1971. Many features available today, were available to users in the 1980's.
*️⃣ DISCLAIMER |
---|
We do not claim suitability of these resources for your requirements. No affiliation with associated individuals, groups, companies, or ideologies, exists. We do not necessarily endorse, nor are we endorsed by, any of the aforementioned. (WANL) |