Linuxlab

Bash: writing scripts

In this lesson you write a few small bash scripts and check them right in the terminal. Bash is both a shell and a language: anything you type by hand can go into a file and run as many times as you like.

What is already set up

The sandbox is not empty. Your home directory already holds ~/data/words.txt with four words, one per line. You will count its lines and loop over it.

cat ~/data/words.txt
alpha
beta
gamma
delta

Steps

1. Your first script

Make a file ~/count.sh that prints the number of lines in ~/data/words.txt. The first line is the shebang: it tells the kernel what to run the file with. Then give the file the execute bit and run it.

cat > ~/count.sh <<'EOF'
Locked

The rest of this lesson is part of a paid course

You are reading the open part. Buy the course to unlock the whole lesson and its lab.

See the course