Scripts and Startup
This lesson will cover two related subjects: Shell scripts and what happens when the shell is started up.
You’ve probably come across shell scripts, which won’t take long to cover, but shell startup is a useful but tricky topic that catches most people out at some point, and is worth understanding well.
How Important is this Lesson?
This lesson is essential to the course.
Shell Scripts
First create a folder to work in and move into it:
A shell script is simply a collection of shell commands that can be run non-interactively. These can be quick one-off scripts that you run, or very complex programs.
The Shebang
Run this:
echo '#!/bin/bash' > simple_scriptecho 'echo I am a script' >> simple_script
You have just created a file called simple_script
that has two lines in it.
The first consists of two special characters: the hash and the exclamation mark.
This is often called shebang, or hashbang to make it easier to say. When
the operating system is given a file to run as a program, if it sees those
two characters at the start, it knows that the file is to be run under the
control of another program (or interpreter as shells are often called).
Now try running it:
./simple_script
That should have failed. Before we explain why, let’s understand the command.
The ./
characters at the start of the above command tells the shell that you
want to run this file from within the context of the current working directory.
It’s followed by the filename to run.
Similarly, the ../
characters indicate that you want to run from the directory
above the current working directory.
This:
mkdir tmpcd tmp../simple_script # Call the script from the tmp subfoldercd ..rm -rf tmp
will give you the same output as before.
The Executable Flag
The script in the above example will have failed because the file was not marked as executable ...