If, metaphorically speaking, a shell pipeline is kind of like an incantation or minor spell that you come up with on the spot to solve some problem, then scripts are a lot like the pages of a spellbook where you keep the incantations that you've found really useful.

If you're familiar with a general-purpose programming language like Python, C, Ruby, or JavaScript, then maybe you've noticed by now that Bash has some familar features. That's because the shell is a programming language.

Just about any sequence of shell commands you'd type at the prompt can be stashed in a simple text file and executed as a program.

What's more, Bash has many of the features you'd expect of a programming language:

variables

export FOO=bar

echo $FOO

loops

for file in `ls`; do echo "file: $file"; done

conditionals

if [ "`date '+%H'`" -lt 6 ]; then
  echo "do not feed the mogwai"
fi

functions

function moo() {
   cowsay moo
}

input handling

while read thought; do
  cowthink "$thought"
done

Between these constructs and the features provided by the standard utilities, shell scripts can be used to solve all sorts of problems. For example:

A Sample Script

Let's assemble a very simple filter script that turns all the non-space characters in its input into little stars. First, open stars.sh in Nano:

nano stars.sh

Next, type or paste the following lines and hit Ctrl-x to save the file.

#!/bin/bash
sed 's/\S/★/g' /dev/stdin

To break this down:

  • #!/bin/bash is what's known as a shebang or a hashbang. #! are special characters that tell the kernel "this file should be run by feeding it to the following program". (You'll also commonly see this written as #!/usr/bin/env bash, which is sometimes considered a more portable way to invoke an interpreter.)
  • sed is the stream editor. It acts as a filter, taking a stream of text in one side and transforming it by running one or more commands.
  • s/\S/★/g is a command that says, more or less, "substitute strings matching this regular expression (\S) with this replacement string (), globally". The \S will match everything that's not whitespace in its input.
  • \s (lowercase) is the metacharacter for space characters; think of the uppercase version as inverting this.
  • /dev/stdin is a special file that contains the standard input to our script.

Now make the file executable, and give it a try...

chmod +x ./stars.sh

Hit Enter to submit text, and Ctrl-d on a line by itself to end input. You could also, just as easily, pipe the output of some other command to ./stars.sh.

Further Reading

This guide was first published on Feb 24, 2015. It was last updated on Feb 24, 2015.

This page (Write Shell Scripts) was last updated on Feb 23, 2015.

Text editor powered by tinymce.