Bash (short for Bourne Again SHell) is a command-line interpreter that allows you to interact directly with your computer’s operating system.
Using Bash, you can manage files, automate tasks, and build powerful workflows — all by typing simple text commands.
Learning Bash is a rite of passage for anyone serious about mastering Unix-like systems.
The following commands form the foundation of working in a Bash environment.
| Command | Description | Example |
|---|---|---|
pwd |
Display the current working directory | pwd |
ls |
List files and directories | ls -l |
cd |
Change directory | cd ~/Documents |
mkdir |
Create a new directory | mkdir NewFolder |
touch |
Create a new, empty file | touch notes.txt |
cp |
Copy files or directories | cp source.txt destination.txt |
mv |
Move or rename files or directories | mv oldname.txt newname.txt |
rm |
Remove files | rm file.txt |
rmdir |
Remove empty directories | rmdir EmptyFolder |
You can clear the terminal screen using:
clearVim is a powerful text editor built directly into the terminal. To create or edit a file:
vim myfile.shBasic Vim commands:
- Press
ito enter Insert mode (start typing). - Press
Escto exit Insert mode. - Type
:wto save the file. - Type
:qto quit Vim. - Type
:wqto save and quit in one step.
Open a new file using Vim:
vim myscript.shInside, write the following:
#!/bin/bash
echo "Hello, world! Welcome to Bash scripting."The first line (
#!/bin/bash) is called the shebang — it tells the system to use Bash to run the script.
Before running the script, you need to make it executable:
chmod +x myscript.shExecute your script:
./myscript.shYou should see:
Hello, world! Welcome to Bash scripting.
Congratulations — you have written and executed your first Bash script.
| Concept | Description |
|---|---|
# |
A comment. Used to explain your code. |
echo |
Prints text to the terminal. |
read |
Takes input from the user. |
Variables ($VAR) |
Store and reuse data. |
if, then, else, fi |
Conditional logic (decisions). |
for, while, do, done |
Loops (repeat actions). |
#!/bin/bash
echo "Enter your name:"
read username
echo "Welcome, $username!"#!/bin/bash
echo "Enter a number:"
read number
if [ $number -gt 10 ]; then
echo "The number is greater than 10."
else
echo "The number is 10 or less."
fi#!/bin/bash
for i in {1..5}
do
echo "Iteration number $i"
done-
&&→ Run the next command only if the first one succeeds:mkdir TestFolder && cd TestFolder
-
||→ Run the next command only if the first one fails:cd Nonexistent || echo "Directory does not exist."
-
;→ Run multiple commands sequentially:echo "First command"; echo "Second command"
-
$(...)→ Capture the output of a command:echo "Today is $(date)"
Learning Bash is more than learning a set of commands — it is about thinking systematically and automating your work.
It turns the computer from a passive machine into a powerful tool that bends to your will.