Web Analytics

First Bash Script

Beginner ~15 min read

Now let's write your first Bash script! A Bash script is simply a text file containing Bash commands that can be executed like a program. You'll learn about the shebang line that tells the system which interpreter to use, how to make scripts executable, and different ways to run your scripts.

The Shebang (#!)

The shebang (also called hashbang) is a special line at the beginning of a script that tells the operating system which interpreter to use. It always starts with #! followed by the path to the interpreter.

Output
Click Run to execute your code
Common Shebang Lines:
- #!/bin/bash - Use Bash interpreter
- #!/bin/sh - Use POSIX-compliant shell (more portable)
- #!/usr/bin/env bash - Use bash from PATH (more flexible)
- #!/usr/bin/python3 - For Python scripts
The shebang must be on the first line, and there should be no spaces before it!

Your First "Hello World" Script

Let's create a simple script that prints "Hello, World!" to the screen. This is the traditional first program in any language!

Output
Click Run to execute your code
Pro Tip: The echo command is Bash's way of printing output. It's similar to print() in Python or console.log() in JavaScript. You'll use it constantly in Bash scripts!

Making Scripts Executable

Before you can run a script directly (like ./script.sh), you need to make it executable using the chmod command. This grants execute permissions to the file.

# Make a script executable
chmod +x script.sh

# Or be more specific (read, write, execute for owner)
chmod 755 script.sh

# Check file permissions
ls -l script.sh
Permission Symbol Numeric Description
Read r 4 Can view file contents
Write w 2 Can modify file contents
Execute x 1 Can run file as program
Understanding chmod:
- chmod +x adds execute permission for all (user, group, others)
- chmod 755 means: owner can read/write/execute (7), group and others can read/execute (5)
- The first digit is for owner, second for group, third for others
- 7 = 4+2+1 (read+write+execute), 5 = 4+1 (read+execute)

Running Bash Scripts

There are several ways to execute a Bash script. Each method has its use cases:

Method Command When to Use
Direct execution ./script.sh Script is executable and in current directory
Using bash bash script.sh Script doesn't need execute permission, or you want to specify interpreter
With sh sh script.sh For POSIX compatibility or when bash isn't available
Source/Execute source script.sh or . script.sh Run in current shell (affects current environment, useful for config files)
Important: The difference between ./script.sh and source script.sh:
- ./script.sh runs in a new subshell - changes don't affect your current session
- source script.sh runs in the current shell - variables and functions become available in your session
Use source for configuration files, use ./ for standalone scripts!

Comments in Bash

Comments help explain your code to others (and to yourself later!). In Bash, everything after # on a line is ignored by the interpreter, except when the # is part of a string or the shebang.

#!/bin/bash
# This is a single-line comment

# Multiple single-line comments
# can explain complex logic
# across several lines

echo "Hello"  # Inline comment - explains this line

# Block comments in Bash require # on each line
# There's no /* */ style block comment like in C/Java
# But you can use multi-line comments with : '...'
: '
This is a multi-line comment
that spans several lines
using the null command
'
Comment Best Practices:
- Explain why you're doing something, not what (code should be self-explanatory)
- Use comments for complex logic, non-obvious workarounds, or important notes
- Keep comments up-to-date when code changes
- Don't over-comment simple, obvious code!

Common Mistakes

1. Missing shebang or wrong shebang

# Wrong - no shebang
echo "Hello"
# Script may work but is less portable

# Wrong - space before shebang
 #!/bin/bash
echo "Hello"

# Correct
#!/bin/bash
echo "Hello"

2. Forgetting to make script executable

# Creating script
echo '#!/bin/bash
echo "Hello"' > script.sh

# Wrong - trying to run without permissions
./script.sh
# Error: Permission denied

# Correct - make executable first
chmod +x script.sh
./script.sh
# Or run with: bash script.sh

3. Wrong path in shebang

# Wrong - bash might be in /usr/bin/bash on some systems
#!/bin/bash

# Better - more portable
#!/usr/bin/env bash

# Check where bash is located
which bash

4. Windows line endings (CRLF) causing issues

# Wrong - Windows line endings (CRLF) cause: bad interpreter error
#!/bin/bash\r
echo "Hello"

# Correct - Unix line endings (LF)
#!/bin/bash
echo "Hello"

# Fix with: dos2unix script.sh or sed -i 's/\r$//' script.sh

Exercise: Create and Run Your Script

Task: Create a Bash script that introduces yourself!

Requirements:

  • Start with the shebang line
  • Print your name
  • Print the current date
  • Print your current directory
  • Include at least two comments explaining what the script does
  • Make it executable and run it

Hint: Use date and pwd commands. Don't forget the shebang!

Show Solution
#!/bin/bash
# Introduction script
# This script introduces the user and shows system information

# Print name
echo "Hello! My name is Alex."
echo ""

# Print current date
echo "Today's date is:"
date
echo ""

# Print current directory
echo "I am currently in:"
pwd

Summary

  • Shebang: #!/bin/bash on the first line tells the system which interpreter to use
  • Comments: Use # for single-line comments; everything after # is ignored
  • Executable Permission: Use chmod +x script.sh to make scripts executable
  • Running Scripts: ./script.sh (direct), bash script.sh (via interpreter), or source script.sh (in current shell)
  • File Extensions: Use .sh extension for clarity (though not required)
  • Line Endings: Use Unix (LF) not Windows (CRLF) line endings
  • Portable Shebang: #!/usr/bin/env bash is more portable than #!/bin/bash

What's Next?

Great job on creating your first Bash script! In the next lesson, we'll explore Terminal Basics - essential commands like cd, ls, pwd, and file operations that you'll use constantly when working with Bash and the command line.