How to Write Your First Bash Script (Hello World)
DodaTech
1 min read
In this tutorial, you'll learn about How to Write Your First Bash Script (Hello World). We cover key concepts, practical examples, and best practices.
The Problem
You type commands into the terminal one at a time. You want to automate a sequence of commands in a reusable script.
Quick Fix
1. Create the script file
touch myscript.sh
2. Add the shebang and commands
echo '#!/bin/bash' > myscript.sh
echo 'echo "Hello, World!"' >> myscript.sh
echo 'pwd' >> myscript.sh
echo 'ls -la' >> myscript.sh
Or open it in a text editor and write:
#!/bin/bash
echo "Hello, World!"
echo "Current directory: $(pwd)"
echo "Files:"
ls -la
3. Make it executable
chmod +x myscript.sh
4. Run it
./myscript.sh
Expected output:
Hello, World!
Current directory: /home/user/project
Files:
total 12
drwxr-xr-x 2 user user 4096 ...
-rwxr-xr-x 1 user user 103 ...
Variables and Input
#!/bin/bash
name="$1" # First argument
echo "Hello, $name!"
Run with: ./myscript.sh Alice → Hello, Alice!
Adding to PATH
Move the script to a directory in your PATH so you can run it from anywhere:
mkdir -p ~/bin
mv myscript.sh ~/bin/
export PATH="$HOME/bin:$PATH"
Add the export line to ~/.bashrc for permanence.
← Previous
How to Open Ports with firewalld on CentOS/RHEL
Next →
First Input Delay (FID) Improvement Fix
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro