Understand that hard to decipher shell scripting tricks in BASH, ZSH, and ASH.

Member-only story

Quick Tut: Shell Scripting Tricks

Understand those secret shell script tricks with this quick and easy tutorial.

Eric Fossas
7 min readDec 21, 2018

--

I’m sure you’ve seen some strange stuff when looking at shell scripts. This tutorial is a just a quick overview of some those secret tricks that are hard to figure out what they are with just a Google search. I primarily use the BASH, ZSH and ASH shells, so these are things that I believe work with those; I won’t specifiy whether these things work in other shells or whether they are POSIX.

If you’re writing things like Docker entrypoint.sh scripts (which is what I’ve been doing a lot of lately), these knowing this stuff will really come in handy.

Here is some basic shell terminology you need to know.

  • Parameter: A parameter is essentially just a variable, except BASH distinguishes between Positional Parameters, the parameters supplied to the script on the command line, like:

./my_script.sh these are positional parameters

  • and Variable Parameters, the parameters defined inside the script, like:

my_variable_parameter=’hello’;

  • Word: A word is just a string.
  • Pattern: A regex matching pattern.

What This Tutorial Covers

  • Tests
  • Redirection & Piping
  • Parameter & Other Expansions
  • Subshells
  • The Set Command
  • Special Variables

What You Need For This Tutorial

  • A terminal if you want to play around with these tricks.
Most of these tricks help get things done faster and simpler, although at the expense of easy comprehension.

Tests

By tests, I’m just referring to how ‘if’ evaluates expressions. The way shell scripts…

--

--

No responses yet

Write a response