Linux Bash Scripts – Write, Run & Automate Your First Shell Script!

Ready to automate your Linux workflow?
In this video, we’ll show you how to write your very first Bash script from scratch.
From creating a simple “Hello, world!” script to using variables, user input, if-statements, loops, and command-line arguments—this tutorial covers everything you need to start automating repetitive tasks and taking control of your terminal like a pro!

Learn:

✅ Step-by-Step Guide:

1.Your First Script (hello.sh):
#!/bin/bash
echo “Hello, world!”
Save it, make it executable, and run it:

chmod +x hello.sh
./hello.sh
# Output: Hello, world!
Note: #!/bin/bash is called a “shebang” and tells Linux which shell to use.

2.Variables:
#!/bin/bash
name=”Linux”
echo “Hello, $name”
# Output: Hello, Linux
Note: No spaces around the = sign!

3.Read User Input:
#!/bin/bash
read -p “Your name: ” name
echo “Hi, $name”

4.If Statements (Conditional Logic):
#!/bin/bash
age=20
if [ $age -ge 18 ]; then
echo “Adult”
else
echo “Minor”
fi

Common Test Operators:
Test Meaning
-eq Equal
-ne Not equal
-gt Greater than
-lt Less than
-ge Greater than or equal

5.Loops:
# Loop through numbers
for i in 1 2 3 4 5; do
echo “Number $i”
done

# Loop through files
for file in *.txt; do
echo “Found: $file”
done

6.Command-Line Arguments:
#!/bin/bash
echo “First arg: $1”
echo “Second arg: $2”
Run it:

./script.sh apple banana
# Output:
# First arg: apple
# Second arg: banana

7.Mini Example: Backup Script:
#!/bin/bash
mkdir -p ~/backups
cp *.txt ~/backups/
echo “Backup done”

✅ Why Use Bash Scripts?
Automation: Turn repetitive, multi-step terminal commands into a single, reusable script.
System Administration: Essential for managing servers, automating backups, and monitoring system health.
Speed & Simplicity: Bash is pre-installed on virtually every Linux system, making it the fastest way to automate tasks without installing extra tools.
Pipeline Power: Combine commands, variables, and logic to build powerful data-processing pipelines.

✅ Pro Tips:
Always Use the Shebang: Start every script with #!/bin/bash (or #!/bin/sh) to ensure it runs in the correct shell environment.
Quote Your Variables: Always use “$variable” instead of $variable to prevent word-splitting and globbing errors, especially if the variable contains spaces.
Test Before Running: Use bash -n script.sh to check for syntax errors, and bash -x script.sh to debug by printing each command as it executes.
Use set -e: Add set -e at the top of your script to make it exit immediately if any command fails, preventing cascading errors.
Keep Scripts Modular: Break large tasks into functions. Use function_name() { … } to organize your code and make it reusable.

You can find the full tutorial, script examples, and practice exercises here:
📁 https://gitlab.com/hatem-badawi/linuxhowto/-/tree/main/bash/bash-scripts-basics

Perfect for Linux beginners, sysadmins, and developers who want to automate their workflow with the power of the shell!
Hit subscribe for more Linux tips and like if this helped.
Let us know: What’s the first task you plan to automate with Bash?

👉 Watch now and start writing your first Bash script!

#LinuxTips #BashScripting #ShellScript #Automation #CommandLine #LearnLinux #DevOps #SysAdmin

(Short, clear, and packed with practical knowledge!)