For every journey to begin, we need to know how to setup the system for use. Fortunately, POSIX is simpler to setup since every UNIX-like operating system comes with it. Throughout the course, you can use your favorite text editor for your writing your script.
To work with POSIX shell, you need to understand its origin and how program works in *nix environment.
POSIX SHELL programming languages is mainly relying on the *nix pipeline management tool. In another word, it connects an input command, output of the command into another command's input like the way we join water pipes. This conforms the UNIX or Linux filesystem and its concepts. Here's an example:
Let's list all the files and directory using ls:
$ lsbin  code_development  Desktop  Documents  Downloads  Music  Pictures  pkg  Public  snap  src  Templates  VideosNext, let's pass it to a filter, say we want to remove "Desktop" from the ls output, we use pipe "|" command:
$ ls | grep -v "Desktop"bincode_developmentDocumentsDownloadsMusicPicturespkgPublicsnapsrcTemplatesVideosNext, let's say we want to modify the output to be a human dialog, we pass the filtered output again to xargs with echo command:
$ ls | grep -v "Desktop" | xargs -I {} echo "Here is a file:" {}Here is a file: binHere is a file: code_developmentHere is a file: DocumentsHere is a file: DownloadsHere is a file: MusicHere is a file: PicturesHere is a file: pkgHere is a file: PublicHere is a file: snapHere is a file: srcHere is a file: TemplatesHere is a file: VideosNotice the output has changed by piping the first command (ls) into the second command (grep) then lastly xargs-echo command. That's pipeline and almost all UNIX and LINUX system is based on this concept. POSIX shell leverages this concept to reach its maximum potentials.
Another way to imagine shell scripts is like a video camera capturing an action moment. You can view the action repeatedly in the video and it will perform the exact same action every single time.
The difference here is that POSIX shell is a terminal command capturing tool. It captures the list of commands (action) you want and packed it into a script. Then, you execute this script to repeat the set of commands again and again, consistently. Here's an example for scripting a Go programming language installation from their official site:
#!/bin/shgo_version="$1"arch="$(dpkg --print-architecture)"go_pack="go${go_version}.linux-${arch}.tar.gz"go_install_path="/usr/local/go"go_pack_url="https://dl.google.com/go/$go_pack"sudo rm -rf "$go_install_path" > /dev/nullwget "$go_pack_url"tar -xvf "$go_pack"sudo mv go "$go_install_path"rm "./$go_pack"mkdir -p "${HOME}/src"mkdir -p "${HOME}/pkg"mkdir -p "${HOME}/bin"This way, every-time when there is a new version of Go releases, I can save my time to run this script instead of typing each line of commands in it.
POSIX shell, by itself, doesn't have any working feature modules. It relies heavily on the executable programs available on a Unix system to get the job done.
In another word, POSIX shell potentials become very limited if the core executables are not available, like coreutils or busybox. These libraries are the one responsible for facilitating commands like echo, [[, eval, grep, etc. Hence, always keep in mind that every SHELL script depends heavily on the operating system's facilities.
That being said, on the contrary, it very easy to learn BASH since there is only a little handful of things and conventions for you to remember.
Thankfully, most UNIX system already packed the core packages into it. You can try using which command to identify a particular command's availability.
$ which echo/bin/echo$If you want to know which package is responsible for this executable, use your package manager to identify it. Here is an example checking echo command from debian package manager using dpkg:
$ dpkg -S /bin/echo coreutils: /bin/echo$ dpkg -s coreutils Package: coreutilsEssential: yesStatus: install ok installedPriority: requiredSection: utilsInstalled-Size: 15103Maintainer: Michael Stone <mstone@debian.org>Architecture: amd64Multi-Arch: foreignVersion: 8.26-3Replaces: mktemp, realpath, timeoutPre-Depends: libacl1 (>= 2.2.51-8), libattr1 (>= 1:2.4.46-8), libc6 (>= 2.17), libselinux1 (>= 2.1.13)Conflicts: timeoutDescription: GNU core utilities This package contains the basic file, shell and text manipulation utilities which are expected to exist on every operating system. . Specifically, this package includes: arch base64 basename cat chcon chgrp chmod chown chroot cksum comm cp csplit cut date dd df dir dircolors dirname du echo env expand expr factor false flock fmt fold groups head hostid id install join link ln logname ls md5sum mkdir mkfifo mknod mktemp mv nice nl nohup nproc numfmt od paste pathchk pinky pr printenv printf ptx pwd readlink realpath rm rmdir runcon sha*sum seq shred sleep sort split stat stty sum sync tac tail tee test timeout touch tr true truncate tsort tty uname unexpand uniq unlink users vdir wc who whoami yesHomepage: http://gnu.org/software/coreutils$In case you need a particular command, you'll need to search it out and install it into the system. There are various ways to install an open-source software. Perhaps, you can start with package manager.
POSIX relies on other POSIX-compatible shells like BASH or DASH. Therefore, you need to follows the setup available in their respective setup guide. I wrote BASH guide. DASH is similar to BASH so the guide is applicable for both.
That's all about setting up POSIX compatible shell environment. Feel free to visit the next sub-topic.