A bash script hello world example is the first thing every Linux beginner must write to understand how shell scripting works. Only a couple lines of code may be able to automate a process that would otherwise have to be done manually. Learning how to create a Bash Script Hello World Step By Step is an excellent way to begin using the Shell as a user on a Linux system. This article will walk you through Creating, Running, and Understanding Permissions of your First Shell Script with Real Examples.
Why Your First Script Matters:
Most beginners know Linux commands but never connect them into a script. To take that knowledge and turn it into real control over Linux is to fill that gap.
- A single bash script can replace 10 manual commands run every day
- Permissions and execution are the two most misunderstood parts for beginners
- Getting hello world right teaches you the structure used in every script you will ever write
If you can write, execute, and fix your first script, you can automate anything on Linux.
The first step for those ready to start using their knowledge in this way is to learn about how Bash (or sh) searches for and then runs commands. If you want to "catch up" or are new to searching and running commands with Bash, we have a detailed post on Bash PATH explained and command lookup, which covers exactly how your terminal finds executables.
What is a Bash Script Hello World and Why Write One
Bash Scripts are simple text files that contain a list of bash commands. Each time you run a bash script, the bash shell will read the file one line at a time and execute each command in sequence. Because bash scripts execute commands sequentially, they offer the most power when used as an alternative to entering multiple manual commands to accomplish a task. For example, instead of manually entering 5 commands every morning, you create a single file containing all of those commands and enter "run.sh" (assuming the file name) in your Terminal.
Explanation:
The word "script" simply means a set of instructions written in a specific order. A bash script is no different from a recipe. You list steps, and Linux follows them. The #!/bin/bash line at the top tells Linux which interpreter to use, which is Bash in this case. This line is called the shebang line and it must always be the very first line of your script.
You can review beginner-level command usage on our Linux commands for beginners page to strengthen your base before scripting.
LinuxTeck.com
# This is my first bash script
echo "Hello, World!"
Three lines. That is the complete structure of a working shell script. Line one sets the interpreter. Line two is a comment that Bash ignores. Line three prints output to the screen. Every advanced script you will ever write follows this exact same pattern at its core.
How to Create a Bash Script File Step by Step
Creating a script file is one of the most important steps beginners need to get right. You can use any text editor available on your system. The file name should end with .sh by convention, though Linux does not require this extension to run a script.
Step by Step File Creation:
Use nano to create and open the file in one command. It opens a terminal editor where you can type your script immediately. Once you are done, press Ctrl + O to save and Ctrl + X to exit.
For a full reference on text editor commands, see our Linux text editors cheat sheet.
LinuxTeck.com
Inside the editor, type this script:
LinuxTeck.com
# hello.sh - My first shell script
echo "Hello, World!"
echo "Today is: $(date)"
echo "Logged in as: $USER"
Today is: Mon Apr 28 10:35:21 IST 2026
Logged in as: john
Tip:
The $(date) syntax is called command substitution. It runs the date command inside your echo statement and inserts the result directly into the output. This is one of the most used patterns in real-world shell scripting. The $USER variable is a system environment variable that always holds the current logged-in username.
How to Make a Bash Script Executable Using chmod
After creating your script, Linux will not run it until you give it execute permission. This is a security design. Every file on Linux has three types of permissions: read, write, and execute. A new file created with a text editor has no execute permission by default. You must add it manually using the chmod command.
LinuxTeck.com
To verify that the permission was applied correctly, use ls -l:
LinuxTeck.com
Reading the Permission String:
The string -rwxr-xr-x tells you everything about who can do what with the file.
The first character is the file type (dash means regular file). The next three characters rwx are the owner permissions (read, write, execute).
The next three r-x are group permissions.
The final three r-x are permissions for everyone else. The x in each set is the execute bit that chmod +x added.
For a deeper dive into all chmod options, see our chmod command in Linux guide.
Common Mistake:
Trying to run the script without setting execute permission gives this error:
bash: ./hello.sh: Permission denied
Fix: chmod +x hello.sh — run this once and the file stays executable until you manually remove the permission.
How to Run a Shell Script in Linux (3 Methods)
Once your script is ready and has execute permission, there are three ways to run it. Each method works differently and knowing which one to use matters in different situations.
Method 1: Using ./ (Recommended for Beginners):
This tells Linux to look for the script in the current directory. Without ./, Linux searches only the directories in your PATH variable and will not find your script in the current folder.
LinuxTeck.com
Today is: Mon Apr 28 10:40:05 IST 2026
Logged in as: john
Method 2: Using bash directly (No chmod needed):
You can pass the script as an argument directly to the bash interpreter. This bypasses the permission check entirely. It is useful for testing a script before making it executable. Note that this still requires the file to be readable.
LinuxTeck.com
Method 3: Using sh (POSIX compatible):
This runs the script using the default system shell, which may not be Bash. If your script uses Bash-specific features like arrays or [[ ]] conditions, this method may produce errors. Use bash explicitly to avoid that.
For a complete reference on shell scripting commands, bookmark our Linux shell scripting command cheat sheet.
LinuxTeck.com
Real-World Bash Script Example Beyond Hello World
Hello World shows you the structure. But a real-world example shows you the value. Here is a practical linux shell script example that checks your system's disk usage and prints a warning if any partition is above 80 percent. This is the kind of script that sysadmins actually use every day.
LinuxTeck.com
# disk-check.sh - Alert when disk usage is high
THRESHOLD=80
echo "Disk Usage Report - $(date)"
echo "-------------------------------"
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read usage filesystem; do
usage_num=${usage%\%}
if [ "$usage_num" -ge "$THRESHOLD" ]; then
echo "WARNING: $filesystem is at $usage - above threshold!"
else
echo "OK: $filesystem is at $usage"
fi
done
-------------------------------
OK: /dev/sda1 is at 45%
WARNING: /dev/sdb1 is at 83% - above threshold!
OK: /dev/sdc1 is at 61%
Why This Script Works:
This script uses four real commands piped together.
The df -H command lists disk usage.
The grep filters out header lines and temporary filesystems.
The awk extracts only the usage percentage and filesystem name.
The while loop then processes each line and compares the number against the threshold.
Each of these is a building block you will reuse in dozens of future scripts. For disk usage command details, see our df command in Linux with examples.
Common Mistakes When Writing Your First Bash Script
Most beginners hit the same set of errors on their first script. Knowing these in advance saves hours of frustration. Here are the four most common mistakes and exactly how to fix each one.
Mistake 1: Missing the Shebang Line:
Forgetting #!/bin/bash on the first line means Linux may use the wrong shell to run your script. Some systems default to sh, which does not support all Bash features.
Fix: Always start every bash script with #!/bin/bash as the very first line with no blank line before it.
Mistake 2: Windows Line Endings (CRLF):
If you write a script on Windows and transfer it to Linux, it may fail with a confusing error like bad interpreter: No such file or directory. This happens because Windows uses \r\n line endings and Linux uses only \n.
Fix: sed -i 's/\r//' hello.sh — this strips the carriage return characters from the file.
Mistake 3: Spaces Around = in Variables:
Writing NAME = "John" with spaces around the equals sign causes a syntax error in Bash. Bash treats this as a command named NAME with arguments.
Fix: NAME="John" — no spaces on either side of the equals sign. This is one of the most common beginner errors in bash scripting basics.
Mistake 4: Forgetting chmod Before Running:
Running ./hello.sh without setting permissions gives a Permission denied error. This catches nearly every beginner at least once.
Fix: chmod +x hello.sh — run this once after creating the script. You can find more practical command examples in our basic Linux commands guide.
Interview Questions (Practical)
What is the purpose of #!/bin/bash at the top of a script?
The line #!/bin/bash is called the shebang or hashbang. It tells the operating system which interpreter to use to execute the script. The #! characters are a special marker that Linux recognizes. The path after it, /bin/bash, points to the Bash binary. Without this line, Linux may default to a different shell like sh, which can cause scripts using Bash-specific syntax to fail or behave unexpectedly. You can verify the path of your Bash interpreter by running which bash.
LinuxTeck.com
What is the difference between bash hello.sh and ./hello.sh?
When you run bash hello.sh, you are calling the Bash interpreter directly and passing the script as an argument. This does not require the execute bit to be set on the file. When you run ./hello.sh, you are telling Linux to execute the file directly using the interpreter defined in the shebang line. This method requires the execute permission to be set with chmod +x. For production scripts that other users or cron jobs will run, using ./script.sh with proper permissions is the standard approach. You can review real automation use cases in our guide on Linux bash scripting automation.
How do you check if a bash script has execute permission?
Use ls -l scriptname.sh to check the permission string. If the output starts with -rwxr-xr-x, the execute bit is set for the owner. If it shows -rw-r--r--, no execute permission exists. You can also use the test flag -x inside a script to check programmatically.
LinuxTeck.com
# Or check programmatically:
[ -x hello.sh ] && echo "Executable" || echo "Not executable"
What does command substitution mean in bash scripting?
Command substitution allows you to embed the output of one command inside another. The syntax is $(command). When Bash encounters this, it runs the inner command first, captures its output, and substitutes it in place. For example, echo "Today is $(date)" runs the date command and inserts its result into the echo statement. The older syntax using backticks `command` does the same thing but is harder to read and cannot be nested. Always use $() in modern scripts.
LinuxTeck.com
echo "Uptime: $(uptime -p)"
echo "Kernel: $(uname -r)"
Uptime: up 3 hours, 12 minutes
Kernel: 5.15.0-91-generic
What is the difference between bash and sh when running a script?
sh refers to the POSIX-compliant system shell. On most modern Linux systems, sh is a symlink to dash, not Bash. dash is lighter and faster but does not support Bash-specific features like arrays, [[ ]] extended conditionals, or local variable declarations in functions. If your script uses only basic POSIX commands, both work the same. If it uses Bash-specific syntax, always run it with bash explicitly or set the shebang to #!/bin/bash. For a full interview preparation resource, visit our Linux shell scripting interview questions page.
How do you add comments inside a bash script?
Any line that starts with # is treated as a comment by Bash and is not executed. Comments are essential for documenting what your script does, especially when others will read or maintain it. You can also place inline comments at the end of a command line. The only exception is the shebang line #!/bin/bash, which starts with #! but is not treated as a comment by the OS kernel.
LinuxTeck.com
# This script greets the user
# Author: LinuxTeck | Date: 2026
echo "Hello $USER" # Print current username
Summary
Learning how to write a bash script hello world step by step is the single most important first move in shell scripting. You now know how to create a script file, add the shebang line, set execute permission with chmod +x, and run it using three different methods. For a complete reference as you keep building scripts, see our Linux shell scripting command cheat sheet.
Related Articles
Learn step-by-step how to automate Linux tasks with real-world scripts and practical examples.