Bash Tutorial menu

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

SystemHow to open
Linux (Ubuntu)Press Ctrl + Alt + T
macOSOpen the Terminal app (macOS uses zsh, which runs most Bash commands; type bash to switch)
WindowsInstall 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

bash
echo "Hello, Workownix!"
echo Karachi is a big city
Output
Hello, 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

bash
whoami
date
pwd
CommandWhat it shows
whoamiYour user name
dateToday's date and time
pwdThe folder you are in now (print working directory)
clearClears the screen
historyCommands 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.

  1. Create a file called hello.sh with any text editor, for example nano hello.sh.
  2. Type this inside it:

Example

bash
#!/bin/bash
# My first script
echo "Assalam o Alaikum!"
echo "This script is running in Bash."
  1. Save it, then make it executable and run it:

Example

bash
chmod +x hello.sh
./hello.sh
Output
Assalam o Alaikum!
This script is running in Bash.

The script explained

LineMeaning
#!/bin/bashThe shebang: tells the system to run this file with Bash
# My first scriptA comment. Bash ignores lines that start with #
chmod +x hello.shGives the file permission to run
./hello.shRuns 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?

Learn Bash with a teacher

Join the Python Programming course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp