Bash Tutorial for Beginners

Bash is one of the most widely used command-line shells on Linux systems, and it is also a powerful scripting language. If you are learning Linux, system administration, programming, or automation, Bash is a great place to start.

In this tutorial series, you will learn how Bash works, how to write Bash scripts, and eventually how to use Bash to build useful command-line tools.

Table of Contents

  1. What is Bash?
  2. What Can You Do With Bash?
  3. Bash vs. the Linux Terminal
  4. How to Check Whether Bash Is Installed
  5. How to Start Bash
  6. Your First Bash Command
  7. Listing Files With ls
  8. Getting Help With Commands
  9. Commands vs. Bash Scripts
  10. Your First Bash Script
  11. What Is a Shebang?
  12. Comments in Bash
  13. Making the Script Executable
  14. Running Your Bash Script
  15. Why Learn Bash?
  16. What You’ll Learn in This Bash Tutorial Series
  17. What You Need Before Starting
  18. Using Bash on Windows
  19. Start Learning Bash

What Is Bash?

Bash stands for Bourne Again SHell. It is a command-line shell that lets you interact with your operating system by typing commands. It was created by Brian Fox for the GNU Project in 1988. It was designed as a free and open-source replacement for the older Bourne shell (sh), which was created by Stephen Bourne at Bell Labs.

Bash is extremely common on Linux systems. It is also available on macOS, although modern versions of macOS use Zsh as the default interactive shell. Windows does not include Bash as its normal command shell, but Windows users can still use Bash through tools such as the Windows Subsystem for Linux (WSL).

Bash can be used interactively by entering commands into a terminal, or it can be used as a scripting language to automate commands and repetitive tasks.

What Can You Do With Bash?

You can use Bash to:

  • Navigate through files and directories
  • Create, move, rename, and delete files
  • Install and manage software
  • Automate backups
  • Process files and text
  • Monitor a Linux system
  • Analyze system logs
  • Run sequences of commands automatically
  • Manage processes and services
  • Write automation scripts
  • Build command-line utilities
  • And much more

As you continue through this tutorial series, you will learn how to do many of these things yourself.

Bash vs. the Linux Terminal

One important distinction beginners often miss is that Bash and the terminal are not the same thing.

A terminal is the application or window where you type commands. Think of it as the interface that lets you communicate with command-line programs.

A shell is the program that reads and interprets the commands you type.

Bash is one particular shell.

In other words:

  • The Terminal is where you type commands
  • The Shell is the program that interprets those commands
  • Bash is one specific type of shell

There are many other shells, including Zsh, Fish, Dash, and others.

Note:

In some of the examples in this tutorial, you may see a $ before a command:

$ bash --version

The $ represents the shell prompt. Do not type the $ yourself.

You only need to type:

bash --version

How to Check Whether Bash Is Installed

Open a terminal and run:

$ bash --version

If Bash is installed, you should see output similar to:

GNU bash, version 5.x.x
Copyright (C) Free Software Foundation, Inc.

The exact version number will depend on your system.

If you receive an error such as:

bash: command not found

Bash may not be installed on your system.

It is important to understand that bash --version tells you whether Bash is installed. It does not necessarily tell you which shell your terminal is currently using.

You can check your configured login shell with:

$ echo "$SHELL"

You might see something like:

/bin/bash

or:

/bin/zsh

If you see /bin/bash, Bash is configured as your login shell.

How to Start Bash

Even if your terminal is currently using another shell, you can start Bash by running:

$ bash

This starts a new Bash shell inside your terminal. You can then enter Bash commands normally.

When you are finished, type:

$ exit

This exits the Bash session and returns you to your previous shell. If your terminal is already running Bash, you usually do not need to run bash first.

Your First Bash Command

Let’s run your first command.

Open a terminal and type:

$ echo "Hello, World!"

Press Enter.

You should see:

Hello, World!

You just ran a command through Bash.

Let’s break it down.

The echo Command

echo prints text to the terminal.

In this command:

$ echo "Hello, World!"

echo is the command being executed.

"Hello, World!" is an argument being passed to echo. An argument provides additional information to a command. In this case, it tells echo what text to print. The quotation marks group the text together and prevent certain characters or spaces from being interpreted separately by the shell.

The quotation marks themselves are not printed, which is why the output is:

Hello, World!

instead of:

"Hello, World!"

You will learn much more about commands, arguments, and quotes later in this Bash tutorial series.

Listing Files With ls

Let’s try another command:

$ ls -a

You might see output similar to:

.   ..   .bash_history   .bashrc   Documents   Downloads

Let’s break this command down.

ls

ls means list. It lists files and directories.

Running:

$ ls

normally displays the non-hidden files and directories in your current directory.

The -a Option

In:

ls -a

the -a changes the behavior of ls. The -a option means all, so ls also displays hidden files and directories.

On Linux and other Unix-like systems, files and directories whose names begin with a period (.) are normally considered hidden.

Examples include:

.bashrc
.bash_history
.config

Options beginning with - are often called options, flags, or command-line options. Different commands support different options.

For example:

$ ls -l

shows a more detailed listing, while:

$ ls -a

includes hidden files.

You can even combine many short options:

$ ls -la

You will learn more about commands and options in the next tutorial.

Getting Help With Commands

Bash includes a built-in help command.

Try:

$ help

This displays Bash’s built-in commands.

You can also ask for help about a specific Bash built-in. For example:

$ help echo

However, help does not document every program installed on your computer.

Many external commands support the --help option:

$ ls --help

Linux systems also commonly provide manual pages through the man command:

$ man ls

Press q to exit a manual page.

These tools are extremely useful when working from the command line because you do not have to memorize every option a command supports.

Commands vs. Bash Scripts

So far, you have been using Bash interactively.

You type a command:

$ echo "Hello, World!"

Bash runs it immediately. Bash can also execute commands stored inside a file. This is called a Bash script.

Let’s create one.

Your First Bash Script

Open your preferred text editor or Integrated Development Environment (IDE). I use Visual Studio Code, but you can use almost any plain-text editor.

Create a new file named:

hello_world.sh

The .sh extension is commonly used for shell scripts. Technically, Bash does not require the file to end in .sh, but using the extension makes it easier to identify the file as a shell script.

Add the following:

#!/usr/bin/env bash

# This outputs "Hello, World!"
echo "Hello, World!"

Now save the file. Let’s look at each part before running it.

What Is a Shebang?

The first line is:

#!/usr/bin/env bash

The characters:

#!

are called a shebang.

When you execute the script directly, the shebang tells the operating system which interpreter should be used to run the file. In this example:

#!/usr/bin/env bash

env searches for bash using the environment’s PATH and then runs the script with the Bash interpreter it finds. This is a common way to write Bash scripts because Bash may be installed in different locations on different systems.

Comments in Bash

The next line is:

# This outputs "Hello, World!"

This is a comment. In Bash, a # normally begins a comment. Bash ignores everything after the # on that line. Comments let you leave notes inside your scripts explaining what the code does.

For example:

# Display a greeting
echo "Hello!"

The comment is ignored, while the echo command is executed. Comments become especially useful as your scripts get larger.

Making the Script Executable

Open your terminal and navigate to the directory where you saved hello_world.sh.

Then run:

$ chmod +x hello_world.sh

The chmod command changes file permissions.

In this command:

chmod +x hello_world.sh

chmod is the command. +x tells chmod to add executable permission. hello_world.sh is the file whose permissions you are changing. In simple terms, you are telling the system that this file is allowed to be executed as a program.

Running Your Bash Script

Now run:

$ ./hello_world.sh

You should see:

Hello, World!

You just created and ran your first Bash script. But what does the ./ mean? The . represents your current directory.

So:

./hello_world.sh

Runs the file named hello_world.sh located in the current directory. Linux shells normally do not search the current directory automatically when looking for commands. Using ./ explicitly tells Bash where the executable file is located.

You could also run the script without making it executable by passing it directly to Bash:

$ bash hello_world.sh

In that case, Bash opens and executes the script itself.

Why Learn Bash?

Bash is useful because many computer tasks involve running several commands in a predictable order. Instead of repeatedly typing those commands yourself, you can put them into a script and let Bash do the work for you.

Once you understand variables, conditions, loops, functions, and command-line arguments, Bash can automate surprisingly complex tasks. It is especially useful for Linux administration, servers, development environments, automation, and command-line utilities.

What You’ll Learn in This Bash Tutorial Series

This article is the starting point for the Bash tutorial series.

Throughout the series, you will learn about:

  • Basic Bash commands
  • Variables
  • User input
  • Strings and numbers
  • Command-line arguments
  • If statements
  • Conditions
  • for loops
  • while loops
  • Functions
  • Reusable Bash scripts
  • Working with files
  • Automating Linux tasks

Once you understand the fundamentals, you will also be able to apply what you have learned by building real Bash projects.

These include projects such as:

  • A file metadata command-line tool
  • A Linux system monitor
  • An automatic backup utility
  • A Linux log analyzer

Instead of only learning individual commands, these projects will show you how Bash can be used to solve real problems.

What You Need Before Starting

You do not need much to begin learning Bash.

You should have:

  • A Linux, macOS, or other system with Bash installed
  • Bash
  • A terminal
  • A plain-text editor or IDE
  • Basic familiarity with using a computer

You do not need previous programming experience. We will build up from the fundamentals throughout the series.

Using Bash on Windows

Windows does not use Bash as its normal command shell. Windows primarily provides shells such as PowerShell and Command Prompt.

If you are using Windows and want to follow this Bash series, I recommend installing Windows Subsystem for Linux (WSL). WSL gives you a Linux environment that runs directly on Windows, allowing you to use Bash and Linux commands.

Microsoft provides installation instructions here: Install WSL

Once WSL is installed, you can open your Linux distribution’s terminal and follow along with the Bash tutorials.

Start Learning Bash

Now that you know what Bash is, how commands work, and how to create a basic Bash script, you are ready to start learning the language itself.

Continue to the first tutorial:

Bash Tutorial for Beginners: Variables, Input, and Basic Commands

In the next lesson, you will learn how to work with Bash variables, accept user input, and use the basic commands that you will need as you begin writing your own scripts.

Scroll to Top