Bash Introduction
Bash is a shell: a program that reads the commands you type and runs them.
What is Bash?
- Bash stands for Bourne Again SHell, a newer version of the old Bourne shell
- It was released in 1989 as part of the GNU project
- It is the default shell on most Linux systems
- You use it in a terminal, a window where you type text commands
Opening a Terminal
| System | How to open |
|---|---|
| Linux (Ubuntu) | Press Ctrl + Alt + T |
| macOS | Open the Terminal app (macOS uses zsh, which runs most Bash commands; type bash to switch) |
| Windows | Install WSL with wsl --install, or install Git Bash |
When the terminal opens you see a prompt, often ending in $. It means Bash is waiting for a command.
In this tutorial, lines you type at the prompt are shown without the $. Just type the command and press Enter.
Your First Command
echo prints text to the screen:
Example
echo "Hello, Workownix!"
echo Karachi is a big cityHello, Workownix! Karachi is a big city
Quotes are optional for simple text, but it is a good habit to use them.
A Few Useful Commands
Example
whoami
date
pwd| Command | What it shows |
|---|---|
whoami | Your user name |
date | Today's date and time |
pwd | The folder you are in now (print working directory) |
clear | Clears the screen |
history | Commands you typed before |
Press the Up arrow to bring back the last command, and Tab to complete file names automatically.
Your First Script
A script is a text file full of commands.
- Create a file called
hello.shwith any text editor, for examplenano hello.sh. - Type this inside it:
Example
#!/bin/bash
# My first script
echo "Assalam o Alaikum!"
echo "This script is running in Bash."- Save it, then make it executable and run it:
Example
chmod +x hello.sh
./hello.shAssalam o Alaikum! This script is running in Bash.
The script explained
| Line | Meaning |
|---|---|
#!/bin/bash | The shebang: tells the system to run this file with Bash |
# My first script | A comment. Bash ignores lines that start with # |
chmod +x hello.sh | Gives the file permission to run |
./hello.sh | Runs the script in the current folder |
You can also run a script without chmod by typing bash hello.sh.
Getting Help
Most commands explain themselves. Try man ls to read the manual for ls (press q to quit), or ls --help for a short summary.
Exercise
Exercise
What is the first line #!/bin/bash in a script called?
