Ever wondered how to create your own bash scripts but felt overwhelmed by the process? You're in the right place. Creating and executing bash scripts is one of the fundamental skills every Linux user should master, yet many beginners struggle with the basics of file permissions and script execution.
🎯 What You'll Learn: In this hands-on guide, you'll discover:
- How to create a simple bash script from scratch
- The importance of the shebang (
#!/bin/bash
) line - Different methods to execute bash scripts
- Understanding and managing file permissions with
chmod
- Common execution errors and how to fix them
🚀 Getting Started with Bash Scripts
Bash scripts are simple text files containing a series of commands that the bash shell can execute. They're incredibly powerful for automating repetitive tasks, system administration, and building complex workflows.
Prerequisites
Before we dive in, make sure you have:
- Access to a Linux terminal (CentOS, Ubuntu, or any Linux distribution)
- Basic familiarity with terminal commands like
ls
,cat
, andvi
- Understanding of file system navigation
📝 Step 1: Creating Your First Script
Let's start by creating a simple "Hello World" script. First, we'll create an empty file:
touch hello.sh
The .sh
extension is a convention that indicates this is a shell script, though it's not strictly required for execution.
✅ Best Practice: Always use the .sh
extension for bash scripts to make their purpose clear to other developers and system administrators.
✏️ Step 2: Writing the Script Content
Now let's edit our script using a text editor. We'll use vi
in this example:
vi hello.sh
Inside the editor, add the following content:
#!/bin/bash
echo "Hello World!"
Let's break down what each line does:
Understanding the Shebang (#!/bin/bash
)
The first line #!/bin/bash
is called a "shebang" or "hashbang". It tells the system which interpreter to use when executing the script.
🔍 Why the Shebang Matters
Without the shebang, the system might try to execute your script with the wrong interpreter. You can check your default shell with:
echo $SHELL
Most Linux systems use /bin/bash
by default, but it's always better to be explicit.
The Echo Command
The echo
command simply prints text to the terminal. It's one of the most basic and useful commands in shell scripting.
🏃♂️ Step 3: Executing Your Script
There are two main ways to execute a bash script:
Method 1: Using the Bash Command
bash hello.sh
This method works immediately because you're explicitly telling bash to interpret the file:
$ bash hello.sh
Hello World!
💡 How it works: When you use bash hello.sh
, you're passing the script as an argument to the bash interpreter. The file doesn't need execute permissions for this method.
Method 2: Direct Execution (Requires Execute Permissions)
The more common method is to execute the script directly:
./hello.sh
However, if you try this immediately after creating the script, you'll encounter a permission error:
$ ./hello.sh
bash: ./hello.sh: Permission denied
🔐 Step 4: Understanding File Permissions
Let's examine why we got that permission error by checking the file permissions:
ls -l hello.sh
You'll see output similar to this:
-rw-r--r--. 1 centos9 centos9 32 Sep 4 13:08 hello.sh
📖 Understanding Permission Format
-rw-r--r--
- First character: File type (
-
= regular file) - Next 3: Owner permissions (
rw-
= read, write) - Next 3: Group permissions (
r--
= read only) - Last 3: Others permissions (
r--
= read only)
🔍 Permission Letters
r
= Read permissionw
= Write permissionx
= Execute permission-
= Permission not granted
Notice that there's no x
(execute) permission for anyone. That's why we got the "Permission denied" error.
🛠️ Step 5: Making Your Script Executable
To fix the permission issue, we need to add execute permissions using the chmod
command:
chmod +x hello.sh
Now let's check the permissions again:
ls -l hello.sh
You should see something like:
-rwxr-xr-x. 1 centos9 centos9 32 Sep 4 13:08 hello.sh
Notice the x
permissions have been added! Now we can execute the script directly:
./hello.sh
And you'll see:
$ ./hello.sh
Hello World!
🎉 Success! Your script now runs directly without needing to call bash
explicitly.
⚠️ Security Note: Be cautious when adding execute permissions. Only make files executable when necessary, and consider who needs access to run them.
🔄 Complete Workflow Summary
Let's review the complete process from start to finish:
# 1. Create the script file
touch hello.sh
# 2. Edit the script (using vi, nano, or any text editor)
vi hello.sh
# Add: #!/bin/bash
# Add: echo "Hello World!"
# 3. Check your shell (optional)
echo $SHELL
# 4. Test with bash command (works without execute permissions)
bash hello.sh
# 5. Try direct execution (will fail initially)
./hello.sh # Permission denied
# 6. Check current permissions
ls -l hello.sh
# 7. Add execute permissions
chmod +x hello.sh
# 8. Execute the script directly
./hello.sh # Success!
🚨 Common Pitfalls and Solutions
Permission Denied Error
Symptoms: bash: ./script.sh: Permission denied
Cause: Script file lacks execute permissions
Solution: Add execute permissions with chmod +x script.sh
Command Not Found
Symptoms: bash: script.sh: command not found
Cause: Trying to execute without ./
prefix or script not in PATH
Solution: Use ./script.sh
or move script to a directory in your PATH
Bad Interpreter Error
Symptoms: bad interpreter: /bin/bash: no such file or directory
Cause: Incorrect shebang path
Solution: Verify bash location with which bash
and update shebang accordingly
🎯 Key Takeaways
✅ Remember These Points
- Shebang is Essential: Always start bash scripts with
#!/bin/bash
- Two Execution Methods: Use
bash script.sh
or make executable withchmod +x
- Permissions Matter: Scripts need execute permissions for direct execution
- Use .sh Extension: Follow the convention for clarity and organization
- Security First: Only add necessary permissions and be mindful of who can execute your scripts
📖 Further Reading
Official Resources
Community Resources
🎉 Congratulations! You've successfully created and executed your first bash script. You now understand the fundamentals of script creation, file permissions, and different execution methods.
How did this tutorial help you? Share your thoughts or questions in the comments below!
💬 Discussion
I'd love to hear about your bash scripting journey:
- What's the first practical script you plan to create?
- Have you encountered any permission issues in the past?
- What bash scripting topics would you like me to cover next?
- Any automation tasks you're looking to script?
Connect with me: