Skip to content

incusdata/shs1st

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Shell Script First Wiki

Repository for Shell Scripting Matters


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:

  • misc — Uncategorised scripts.
  • snip — Re-usable, or educational snippets.

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.
ifeliffi 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.

*️⃣ 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)