Labs ICT
โญ Pro Login

Shell Scripts

Typing commands one by one is fine for the first few weeks. But eventually you will find yourself typing the same sequence over and over. That is when you write a shell script โ€” a file full of commands that runs them all in order, like a recipe.

Your first script

A shell script is just a plain text file. The first line must be #!/bin/bash โ€” this is called the shebang. It tells the system which interpreter to use. After that, write your commands one per line.

$ echo '#!/bin/bash' > myscript.sh
$ echo 'echo "Hello from my first script!"' >> myscript.sh
Try it Yourself โ†’

Making scripts executable

A script is just text until you give it execute permission. Use chmod +x to make it runnable. Then you can run it with ./ followed by the filename.

$ chmod +x myscript.sh
$ ./myscript.sh
Hello from my first script!

The ./ is necessary because the current directory is usually not in your PATH. It tells the shell "look right here."

Try it Yourself โ†’

The echo command

echo is your best friend in scripting. It prints text to the terminal. Use it to show progress, print variable values, or just say hello.

$ echo "Starting backup..."
$ echo "Backup complete!"
$ echo "User: $USER"

Double quotes let variables expand. Single quotes print everything literally.

Try it Yourself โ†’

A practical example

Here is a real script that backs up a directory:

#!/bin/bash
echo "Backing up documents..."
cp -r ~/Documents ~/Documents-backup
echo "Done. Backup saved to ~/Documents-backup"

Save it, make it executable, and run it whenever you need a quick backup. Now imagine doing that with a dozen commands and conditional logic โ€” that is where scripting really shines.

Try it Yourself โ†’

๐Ÿงช Quick Quiz

What should the first line of a shell script contain?