Working with the BASH Shell: A Practical Guide for Linux Students
If you spend enough time using Linux, the BASH shell stops being just a black screen where you type commands. It becomes your workspace. It’s where files are managed, processes are controlled, and automation begins.
This guide walks through the core ideas of working with the BASH shell — not in a dry classroom tone, but in a practical way that actually makes sense when you sit down at a terminal.
Understanding Command Input and Output
Every command you run in Linux communicates through three built-in channels These are called file descriptors, and they control how data flows in and out of a command.
-
Standard Input (stdin) – where input comes from (usually your keyboard).
-
Standard Output (stdout) – where normal results go (usually your screen).
-
Standard Error (stderr) – where error messages are displayed.
Most beginners never think about these. But once you understand them, you gain real control over how Linux behaves.
Redirection: Taking Control of Output
By default output appears on your screen. But what if you want to save it to a file instead? That’s where redirection comes in
Example:
ls > files.txt
Now the result of ls is saved inside files.txt instead of being printed on the terminal.
If you only want to capture errors:
ls wrongfile 2> error.txt
The number 2 refers to Standard Error.
You can also separate normal output from error messages:
ls > output.txt 2> error.txt
This becomes especially useful when debugging scripts.
That image visually shows how output is redirected from a command into a file which helps understand the flow.
Pipes: Connecting Commands Together
Redirection sends output to a file Pipes send output directly into another command
Example:
ls -l /etc | less
Here’s what’s happening:
-
ls -l /etcproduces output. -
That output becomes input for
less. -
lessdisplays it page by page.
This simple symbol | is one of the most powerful tools in Linux It allows you to chain commands together and build logic without writing a full script.
That image shows the visual flow from one command to another through a pipe
Shell Variables: Storing Information
Variables allow you to store values inside memory and reuse them later They make scripts dynamic instead of static.
There are three main types of variables in BASH.
Environment Variables
These already exist in your system.
To see them:
set
To display one specific variable:
echo $HOME
Important examples:
-
HOME– your home directory -
PWD– your current working directory
These variables are automatically set by the system and should not be changed casually.
That screenshot shows how environment variables appear when listed.
User-Defined Variables
You can create your own:
NAME=w7gc
echo $NAME
Rules for naming:
-
Must not start with a number
-
Can include letters, numbers, and underscores
-
Conventionally written in uppercase
However variables are only available in the current shell session.
To make them available to child shells:
export NAME
Without export subshells cannot access the variable.
Special Variables and Aliases
Some variables control special behaviors
Example:
-
UMASKcontrols default file permissions. -
aliascreates shortcuts for commands.
Check existing aliases:
alias
For example, cp is often an alias for cp -i, which asks before overwriting files.
Environment Files: Making Settings Permanent
When you close the terminal, everything in memory disappears. That includes your variables.
To make variables permanent, place them in startup files such as:
-
~/.bashrc -
~/.profile -
/etc/profile
These files are automatically executed when you log in.
This ensures your custom settings load every time.
Shell Scripts: Automating Your Work
A shell script is simply a text file containing commands executed in order.
Every script starts with something called a shebang:
#!/bin/bash
This tells the system which interpreter to use.
Running a Script
Method 1:
bash myscript
Method 2:
chmod +x myscript
./myscript
The second method treats it like a regular executable program
Improving Script Readability
If you run multiple commands in a script the output can become messy.
Using echo improves clarity:
echo "Current Date:"
date
You can also use escape sequences:
echo -e "Hello\nWorld"
Common escape sequences:
-
\nnew line -
\ttab
Reading Input from the User
Sometimes scripts need user interaction
Example:
echo "Enter your name:"
read NAME
echo "Welcome $NAME"
The read command takes input and stores it inside a variable.
Decision Making in Scripts
Scripts become powerful when they can make decisions
The if Statement
Basic structure:
if [ condition ]
then
commands
elif [ condition ]
then
commands
else
commands
fi
Key points:
-
Ends with
fi -
elifandelseare optional -
Supports logical operators:
-
-a(AND) -
-o(OR) -
!(NOT)
-
That example shows copying a file only if a directory exists.
The case Statement
Useful when comparing one variable to multiple values:
case $VAR in
1) echo "Option 1" ;;
2) echo "Option 2" ;;
*) echo "Invalid option" ;;
esac
Ends with esac
Shortcut Decisions
Instead of writing full if blocks you can use:
command && command
command || command
-
&&runs second command if first succeeds. -
||runs second command if first fails.
Short and efficient
Loops: Repeating Tasks Automatically
The for Loop
Perfect for lists:
for i in 1 2 3
do
echo $i
done
Using seq:
for i in $(seq 1 5)
do
echo $i
done
The while Loop
Runs while a condition is true:
while [ $COUNT -lt 5 ]
do
echo $COUNT
COUNT=$((COUNT+1))
done
The until Loop
Opposite of while:
until [ $COUNT -ge 5 ]
do
echo $COUNT
COUNT=$((COUNT+1))
done
Functions: Reusable Code Blocks
Functions allow you to group commands into reusable units
Example:
createdir() {
if [ ! -d "$1" ]
then
mkdir "$1"
echo "Directory created."
else
echo "Directory already exists."
fi
}
Call it like this:
createdir testfolder
Functions make scripts cleaner and more organized
GOOD LUCK BUDDY (:

Comments
Post a Comment