Back to course

79. Introduction to Shell Scripting: Shebang and Variables

Linux Basics: From Zero to CLI Hero

Automating Tasks

Shell scripting allows you to group a series of commands together to perform complex, repetitive tasks automatically. This is a fundamental 'hero' skill.

The Shebang Line

Every script should start with a shebang (#!) line, which tells the operating system which interpreter to use to execute the script (usually Bash).

bash #!/bin/bash

Making the Script Executable

After writing the script, you must give it execute permissions using chmod.

bash $ chmod u+x my_script.sh

Variables

Shell variables store data. Variables are defined without dollar signs, but referenced with dollar signs ($).

bash #!/bin/bash

MY_NAME="Alice"

echo "Hello, $MY_NAME!"

You can use command output as a variable using command substitution:

CURRENT_DATE=$(date) echo "Today's date is $CURRENT_DATE"