You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Shell scripting allows you to automate tasks by writing sequences of commands in a file. Bash (Bourne Again Shell) is the default shell on most Linux distributions and the most widely used scripting shell.
#!/bin/bash
# my_first_script.sh — a simple example
echo "Hello, World!"
echo "Today is $(date +%Y-%m-%d)"
echo "You are logged in as: $USER"
chmod +x my_first_script.sh # make it executable
./my_first_script.sh # run it
bash my_first_script.sh # or run explicitly with bash
The first line #!/bin/bash (called the shebang) tells the system which interpreter to use:
| Shebang | Interpreter |
|---|---|
#!/bin/bash | Bash shell |
#!/bin/sh | POSIX-compatible shell |
#!/usr/bin/env python3 | Python 3 |
#!/usr/bin/env node | Node.js |
# Assigning variables (no spaces around =)
name="Alice"
age=30
current_dir=$(pwd)
file_count=$(ls | wc -l)
# Using variables
echo "Name: $name"
echo "Age: $age"
echo "Directory: $current_dir"
echo "Files: $file_count"
# Curly braces for clarity
echo "Hello, ${name}!"
echo "File: ${name}_report.txt"
# Read-only variables
readonly PI=3.14159
# Unsetting variables
unset age
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.