Understanding How Bash Handles Data
One of the first things noticed about Bash is that it behaves very differently from traditional programming languages. There are no strict data types like int or string. In Bash, everything starts as text, and meaning depends entirely on context.
Creating the Script with Vim
A new script file is created directly from the terminal:
vim dtypes_and_operators.sh
After entering Insert Mode, scripting begins.
Working with Variables
Strings
#!/bin/bash
message="Welcome to Bash scripting"
echo $message
Strings are straightforward—plain text stored inside variables. Quotes help preserve spaces and special characters.
Numbers in Bash (Integers)
count=5
echo $count
Even though this looks like a number, Bash still treats it as a string internally. Arithmetic requires explicit syntax.
Arithmetic Evaluation
sum=$(( 4 + 6 ))
echo "The Sum is: $sum"
The $(( )) syntax tells Bash to evaluate the contents as a mathematical expression instead of plain text.
Arrays
Arrays are useful for grouping related values.
colors=("blue" "red" "gree")
echo "First color: ${colors[0]}"
echo "Second color: ${colors[1]}"
echo "All colors: ${colors[@]}"
Output:
First color: blue
Second color: red
All colors: blue red green
This structure makes handling lists much easier compared to individual variables.
Bash Operators
Operators are essential for logic, comparisons, and calculations inside scripts.
Arithmetic Operators
a=13
b=4
echo "Addition: $((a + b))"
echo "Subtraction: $((a - b))"
echo "Multiplication: $((a * b))"
echo "Division: $((a / b))"
echo "Remainder: $((a % b))"
Output:
Addition: 17
Subtraction: 9
Multiplication: 52
Division: 3
Remainder: 1
Bash supports basic arithmetic operators, but only inside arithmetic evaluation syntax.
Comparison Operators
eq means Equal to.
ne means Not equal to.
lt means Less than.
le means Less than or equal to.
gt means Greater than.
ge means Greater than or equal to.
Here are the example in code:
#!/bin/bash
# Define two numbers
a=10
b=20
# Comparison examples
# Equal
if [ $a -eq $b ]; then
echo "$a is equal to $b"
else
echo "$a is not equal to $b"
fi
# Not equal
if [ $a -ne $b ]; then
echo "$a is not equal to $b"
else
echo "$a is equal to $b"
fi
# Less than
if [ $a -lt $b ]; then
echo "$a is less than $b"
fi
# Less than or equal
if [ $a -le $b ]; then
echo "$a is less than or equal to $b"
fi
# Greater than
if [ $a -gt $b ]; then
echo "$a is greater than $b"
fi
# Greater than or equal
if [ $a -ge $b ]; then
echo "$a is greater than or equal to $b"
fi
Output:
10 is not equal to 20
10 is not equal to 20
10 is less than 20
10 is less than or equal to 20
Operators like -gt, -lt, eq, etc. are commonly used inside conditional statements.
String Operators
String comparison works differently from numeric comparison.
text1="Linux"
text2="Bash"
if [ "$text1" != "$text2" ]
then
echo "Strings are different"
fi
Output:
Strings are different
Logical Operators
&& means AND
- Runs the second command only if the first succeeds (exit status 0).
#!/bin/bash
a=5
b=10
[ $a -lt $b ] && echo "$a is less than $b"
Output:
5 is less than 10
|| means OR
- Runs the second command only if the first fails (non-zero exit status).
a=5
b=10
[ $a -gt $b ] || echo "$a is NOT greater than $b"
Output:
5 is NOT greater than 10
! means NOT
- Reverses the result of a condition.
if ! [ $a -eq $b ]; then
echo "$a is NOT equal to $b"
fi
Output:
5 is NOT equal to 10
File Test Operators
-e - Checks if a file exists.
#!/bin/bash
file="example.txt"
if [ -e "$file" ]; then
echo "$file exists."
else
echo "$file does not exist."
fi
-d - Checks if a directory exists.
dir="my_folder"
if [ -d "$dir" ]; then
echo "$dir is a directory."
else
echo "$dir does not exist."
fi
-f - Checks if a file is a regular file.
file="example.txt"
if [ -f "$file" ]; then
echo "$file is a regular file."
else
echo "$file is not a regular file."
fi
-s - Checks if a file is not empty.
file="example.txt"
if [ -s "$file" ]; then
echo "$file is not empty."
else
echo "$file is empty."
fi
In this lesson, I have learned that all variables in Bash are treated as text by default, even when they contain numbers. Performing calculations requires using special syntax like $(( )) to tell Bash to treat the values as numbers. Arrays are useful for storing multiple values in a single variable, which makes managing data much easier. Operators, including arithmetic, relational, logical, string, and file tests, provide ways to compare values and control how scripts behave. Using Vim to write and run scripts directly in the terminal helps practice commands while keeping everything organized.
Tomorrow, I’m planning to review this lesson by studying if-else statements and I'll proceed to loops.
Top comments (0)