Bash First Script
A Bash script is a text file that holds a list of commands. The shell reads the file from top to bottom and runs every line. Writing a script saves typing time and makes any task repeatable.
Think of a Script as a Recipe
A cooking recipe lists steps in a fixed order. A cook follows each step without guessing. A script works the same way, and the shell plays the role of the cook.
+-----------------+ +----------------+ +--------------+ | hello.sh | ---> | Bash shell | ---> | Output on | | (list of | | reads and | | the screen | | commands) | | runs lines | | | +-----------------+ +----------------+ +--------------+
Create the Script File
Open a terminal and create a file named hello.sh with nano:
nano hello.shType these lines inside the file:
#!/bin/bash
# My first Bash script
echo "Hello, World!"
echo "Today is $(date +%A)"Save the file and close the editor.
Understand Each Line
The Shebang Line
The first line #!/bin/bash is called a shebang. It tells the system which program must run the file. The path /bin/bash points to the Bash program.
Comments
A line that starts with # is a comment. Bash ignores comments completely. Writers use them to explain code for future readers.
The echo Command
echo prints text on the screen. Quotation marks keep the words together as one piece of text.
The date Part
The piece $(date +%A) runs the date command and drops the result into the sentence. The result is a weekday name such as Friday.
Run the Script
Two ways exist to run a script file.
Method 1: Call Bash Directly
bash hello.shThis method needs no extra setup.
Method 2: Make the File Executable
Give the file permission to run, and start it with a dot and a slash:
chmod +x hello.sh
./hello.shThe ./ part tells the shell to look in the current folder.
Output:
Hello, World!
Today is FridayWhy Does the Dot and Slash Matter?
The shell searches a fixed list of folders for commands. That list is stored in a variable named PATH. The current folder is not on that list for safety reasons. Typing ./hello.sh gives the exact location, so the shell skips the search.
Script With Variables
Add a variable to make the script friendlier:
#!/bin/bash
name="Asha"
echo "Welcome, $name"
echo "Your folder is $(pwd)"Output:
Welcome, Asha
Your folder is /home/asha/scriptsRun Scripts From Any Folder
Store personal scripts in one folder, such as ~/bin. Add that folder to the PATH list:
mkdir -p ~/bin
mv hello.sh ~/bin/
export PATH="$PATH:$HOME/bin"
hello.shPut the export line inside ~/.bashrc to keep the change after a restart.
Script Anatomy Diagram
#!/bin/bash <-- Shebang: picks the interpreter # My first script <-- Comment: ignored by Bash name="Asha" <-- Variable: stores a value echo "Welcome, $name" <-- Command: does the work
Common Beginner Mistakes
- Spaces around the equals sign break a variable. Write
name="Asha", notname = "Asha". - A missing
chmod +xstep triggers a "Permission denied" message. - Windows-style line endings cause strange errors. Run
dos2unix hello.shto fix them. - A shebang placed on line two gets ignored. Keep it on line one.
Key Takeaways
- A script is a text file with commands, saved with a
.shending. - The shebang line selects the program that runs the file.
- Run a script with
bash file.shor make it executable withchmod +x. - Comments start with
#and explain the code. - The
PATHvariable decides where the shell looks for commands.
