What Is Bash?
Bash stands for Bourne Again SHell.
It is the default shell on most Linux systems.
It allows you to:
- Run commands interactively
- Write scripts (files containing commands) A bash script is simply a text file with Linux commands executed line by line.
First Bash Script
Step 1: Create a file.
vim kervie.sh
This opens vim in Normal mode.
Step 2: Go to insert mode by pressing i.
Step 3: Add this content
#!/bin/bash
echo "Hello, World!"
Step 4: Save and exit
Press Esc then type :wq. to save the file.
Understanding the Script
#!/bin/bash
It is called the shebang.
It tells Linux to use Bash to execute the script.
echo "Hello World!"
Prints text to the terminal.
Making the Script Executable
By default, scripts are not executable. Fix that with:
chmod +x kervie.sh
Running the Script
Run the script like this:
Type ./kervie.sh
Output:
Hello World!
Introducing Variables in Bash
A variable allows you to store data and reuse it throughout your script.
Basic variable syntax:
variable_name=value
Important rules:
No spaces around '='.
Variable names are case-sensitive.
Script with Variables
Let’s create another script.
Step 1: Open a new file with Vim.
vim kvariable.sh
Press i to enter Insert mode.
Step 2: Write the script
#!/bin/bash
name="Kervie Jay Sazon"
echo "Hello, $name!"
Step 3: Save and exit
Press ESC, then:
:wq
Step 4: Make it executable and run it
chmod +x kvariable.sh
./kvariable.sh
Output:
Hello, Kervie Jay Sazon!
How Variables Work?
name="Kervie Jay Sazon"
Stores text in a variable called name.
$name
Retrieves the value stored in name.
Variables make scripts dynamic, reusable, and easier to maintain.
Note:
Do not add spaces around =.
name="Kervie jay"
Don't forget $ when using variables.
Don't forget to make the script executable.
Example:
chmod +x kvariable.sh
Running without ./.
Today, I have learned the basics of Bash scripting by creating my first script with Vim, printing output to the terminal, and understanding how variables work in Bash.
Tomorrow, I’ll continue my Bash scripting journey by learning basic math operations and if statements to introduce logic and decision-making into my scripts.
Top comments (0)