+ - 0:00:00
Notes for current slide
Notes for next slide

Shell Scripting

Learn the basics of shell scripting with Bash.

This material is part of architecture & deployment course for Media Engineering.

You will need

  • A Unix CLI

Recommended reading

1 / 21

What is a script?

Shell Scripting

In a Unix-like operating system, a file that can be executed should be one of the following:

  • A binary file, which contains machine-readable binary code that has been compiled from source code.
  • A script, which is a file containing code that is dynamically interpreted.
2 / 21

How is a script executed?

Shell Scripting > What is a script?

When an executable text file is run, a Unix-like operating system looks for a shebang on the first line. A shebang is a line with the following format:

#!interpreter optional-args

For example, the following is a valid shebang:

#!/bin/bash

In this example, it tells the operating system that the interpreter which should run this file is /bin/bash, meaning that this is a Bash script.

Note that there must not be any space between #! and the path of the interpreter /bin/bash.

3 / 21

What can I put in a script?

!/bin/bash

In a bash script, you can put anything you could type in a Bash shell:

#!/bin/bash
echo Hello World

In a PHP script, you can put any PHP code you want:

#!/usr/bin/php
<?php
echo 'Hello World';
?>

Basically, what you can put in a script depends on the interpreter you're using.

4 / 21

How do I create a script?

!/usr/bin/php

Simply create your script:

$> printf '#!/bin/bash\necho Hello World' > test.sh

The printf (print format) command is similar to the echo command but it has better support for special characters like new lines (\n).

Make it executable:

$> chmod +x test.sh

And run it:

$> ./test.sh
Hello World
5 / 21

All kinds of scripts

!/usr/bin/php

The following are a few examples of shebangs, but it is nowhere near exhaustive:

Shebang Script contents
#!/bin/sh Bourne shell commands
#!/bin/bash Bash shell commands
#!/bin/zsh Z shell commands
#!/usr/bin/node Node.js code
#!/usr/bin/php PHP code
#!/usr/bin/python Python code
#!/usr/bin/ruby Ruby code

Of course, the path to the interpreter must correspond to the actual path of the command used (sh, bash, php, etc). It might differ on your machine. Use which bash to find the location of the Bash executable, for example.

6 / 21

What is shell scripting?

!/usr/bin/php

Shell scripting is the practice of writing scripts that contain series of shell commands that you want to be able to reuse.

Any script with a shell as the interpreter is a "shell script".

A script using PHP as the interpreter is still a script, but it's not a "shell script". It's a PHP script.

7 / 21

Shell script basics

!/usr/bin/php

A few pointers on writing Bash scripts (compatible with most POSIX shells).

8 / 21

Commands

!/usr/bin/php > Shell script basics

You can use any shell command in a shell script:

#!/bin/bash
echo Hello World
date
ls

This script could print:

Hello World
Thu Jan 10 23:46:52 CET 2019
file.txt directory ...
9 / 21

Working directory

!/bin/bash

By default, a script executes in the current shell directory.

You can use cd to move around to other directories:

#!/bin/bash
pwd
cd /home
pwd

This script could print:

/some/where/over/the/rainbow
/home

Assuming it was executed from the /some/where/over/the/rainbow directory.

10 / 21

Variables

!/bin/bash

You can declare and reuse variables in scripts:

#!/bin/bash
FOO=bar
echo $FOO

If your variable contains whitespace (spaces, new lines, etc), be sure to quote it when declaring and using it to avoid issues:

#!/bin/bash
FOO="bar baz"
echo "$FOO"
11 / 21

Store the output of commands

!/bin/bash

You can store the result of a command in a variable by wrapping it with backticks:

#!/bin/bash
FILES=`ls -1`
NUMBER_OF_FILES=`echo "$FILES" | wc -l`
echo There are $NUMBER_OF_FILES files

This script would output 10 if there are 10 files in the current directory.

12 / 21

Environment variables

!/bin/bash

Environment variables are also available as variables in shell scripts:

#!/bin/bash
echo $PATH

To set an environment variable, do it like you would in any Bash shell:

#!/bin/bash
export FOO=bar

Of course, the $FOO environment variable in this example will only be set in the context of this script and its child processes (if any).

13 / 21

Conditionals

!/bin/bash

Bash has a classic if/then/else construct:

#!/bin/bash
FOO="bar"
if [[ "$FOO" -eq "foo" ]]; then
echo FOO is foo
elif [[ "$FOO" -eq "bar" ]]; then
echo FOO is bar
else
echo foo is something else
fi

The [[ ]] syntax is a Bash test construct. Also see Bash other comparison operators.

14 / 21

The test built-in command

!/bin/bash

The test command which comes with Bash is another way to write some conditions:

#!/bin/bash
EMPTY_VAR=
FULL_VAR="full"
FILE="/path/to/some/file"
if test -z "$EMPTY_VAR"; then
echo variable is empty
fi
if test -n "$FULL_VAR"; then
echo variable is not empty
fi
if test -f "$FILE"; then
echo file exists
else
echo file does not exist
fi

See Bash file test operators and other comparison operators.

15 / 21

Loops

!/bin/bash

Bash has a for loop:

for item in one two three; do
echo $item
done

The above code would print:

one
two
three

Bash also has while and until. See loops & branches.

16 / 21

Special variables

!/bin/bash

Bash has a number of special variables which are always available:

Variable Description
$0 Name of the command being executed.
$1 First argument passed to the script on the command line (and so on with $2, $3, etc).
$@ All arguments passed to the script.
$? Exit value of the last executed command.

For example, this script says hello to the name passed as the first argument:

#!/bin/bash
echo Hello $1
17 / 21

The set built-in command

!/bin/bash

The set command is specific to Bash and can be used to toggle its option flags.

For example, the -e option aborts the script if an error occurs, while the -x option prints commands before executing them:

#!/bin/bash
set -ex
echo Hello World
cat file-that-does-not-exist
echo Done

This script could print:

+ echo Hello World
Hello World
+ cat file-that-does-not-exist
cat: file-that-does-not-exist: No such file or directory

Note that each command is printed with a leading + before being executed, and that the script stops as soon as an error occurs (which is not the case by default).

18 / 21

Functions

!/bin/bash

You can isolate pieces of code in a function. The special argument variables $1, $2, etc represent the arguments to the function:

#!/bin/bash
print_hello() {
echo Hello $1
}
print_hello World

This script would print Hello World.

19 / 21

Variable scope

!/bin/bash

Note that normal Bash variables have no scope, i.e. they are available in the whole file and every function.

To declare a variable that is local to a function, use the local keyword:

#!/bin/bash
print_hello() {
local name=$1
echo Hello $name
}
print_hello World
echo $name

This script would print Hello World and an empty line, since $name is only defined within the print_hello function.

20 / 21

What is a script?

Shell Scripting

In a Unix-like operating system, a file that can be executed should be one of the following:

  • A binary file, which contains machine-readable binary code that has been compiled from source code.
  • A script, which is a file containing code that is dynamically interpreted.
2 / 21
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow