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 |
|
loops |
|
conditionals |
|
functions |
|
input handling |
|
Between these constructs and the features provided by the standard utilities, shell scripts can be used to solve all sorts of problems. For example:
- Here's a script adapted from Lady Ada's PiTFT installation guide which does the tedious work of configuring a touchscreen for the Pi.
- Here's one for cross-compiling kernels for the Raspberry Pi.
- raspi-config runs the first time a Pi boots and covers a wide range of configuration options.
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
.
- Advanced Bash-Scripting Guide, by Mendel Cooper.
- Bash Reference Manual
Page last edited February 23, 2015
Text editor powered by tinymce.