Bash

History

The first shells were fairly good programming tools, but not very convenient for users. The C shell added a lot of user conveniences (like the ability to repeat a command you’d just typed), but as a programming lan‐ guage it was quirky. The Korn shell, which came along next (in the early ’80s), added a lot of user conveniences, improved the programming language, and looked like it was on the path to widespread adoption. But ksh wasn’t open source software at first; it was a proprietary software product, and was therefore difficult to ship with a free operating system like Linux.

In the late 1980s, the Unix community decided standardization was a good thing, and the POSIX working groups (organized by the IEEE) were formed. POSIX standardized the Unix libraries and utilities, including the shell. The standard shell was primarily based on the 1988 version of the Korn shell, with some C shell features and a bit of invention to fill in the gaps. bash was begun as part of the GNU Project's effort to produce a complete POSIX system, which naturally needed a POSIX shell. (Albing, Vossen, and Newham 2018, 3)

Gotchas

  cmd 2>&1 | next_command
  # can be replaced with
  cmd |& next_command
  ${name:-default}
  ${name:=default}
  ${name:+altvalue}
  ${name:?errmsg}

String Manipulation

  • String Length:
  ${#name}
  • Removing leading text:
  ${name#pre}
  # or...
  ${name##pre}
  • Remove traling text:
  ${name%post}
  # or...
  ${name%%post}
  • Substrings:
  ${name:offset}
  ${name:offset:length}
  ${name: -offset:length}
  ${name:offset:-length}

References:

Albing, Carl, JP Vossen, and Cameron Newham. 2018. Bash Cookbook: Solutions and Examples for Bash Users. O’Reilly Media, Inc.