Skip to content

Bash

Bash

A reference page for the Bash shell

Bash is a command-line shell commonly used on Linux systems. It allows you to interact with your computer by entering commands into a terminal.

On Raspberry Pi OS, Bash is available by default.

Getting Started

When you open a terminal, Bash displays a command prompt indicating that it is ready for you to enter a command.

The appearance of the prompt varies between systems and can be customized. For example, a prompt might display information such as your username, hostname, or current directory. This could look like:

username@hostname:~ $

Type a command and press Enter to run it.

Paths1/2

A path describes the location of a file or directory.

An absolute path starts from the root directory /:

/home/username/Documents

A relative path starts from your current directory:

Documents/example.txt

Paths2/2

The following have special meanings:

Path Meaning
. Current directory
.. Parent directory (one directory above your current directory)
~ Your home directory
/ Root directory

Directories (Folders)

Current Directory

Your current directory is the directory you are currently working in.

To see your current directory:

Terminal window
pwd

Listing Directory Contents

To see the files and directories inside it:

Terminal window
ls

To include hidden files:

Terminal window
ls -a

Moving to Different Directories

Use cd to move to a different directory:

Terminal window
cd directory

For example:

Terminal window
cd Documents

Creating Directories

Create a directory with mkdir:

Terminal window
mkdir example

Removing Directories

Remove an empty directory:

Terminal window
rmdir example

Files

Viewing a File

Display the contents of a text file:

Terminal window
cat example.txt

For longer files, you can use:

Terminal window
less example.txt

Use the arrow keys to move through the file and press q to quit.

Editing a File

There are multiple text editors available in the terminal. One example is Vim. You can open a text file in Vim with:

Terminal window
vim example.txt

See the Vim reference for the commands needed to edit, save, and quit.

Copying and Moving1/2

Copy a file:

Terminal window
cp source destination

For example:

Terminal window
cp example.txt backup.txt

Copying and Moving2/2

Move a file:

Terminal window
mv example.txt Documents/

mv can also be used to rename a file:

Terminal window
mv example.txt renamed.txt

Removing Files

Remove a file:

Terminal window
rm example.txt

Running Commands

Running Commands as Administrator1/2

Some system changes require administrator privileges. Use sudo before a command to run it with elevated privileges:

Terminal window
sudo command

For example:

Terminal window
sudo apt update

Running Commands as Administrator2/2

You may be asked for your password.

Stopping a Command

To interrupt a command that is currently running:

Ctrl+C

This is useful when a command is hanging or when you need to stop a running program.

Making Your Life Easier

Tab Completion1/2

Press Tab while typing to automatically complete commands, file names, and directory names when possible.

For example, if the current directory contains a directory named Documents, you could type:

Terminal window
cd Doc

Tab Completion2/2

Then press Tab to complete it:

Terminal window
cd Documents/

If there is more than one possible match, press Tab again to display the available options.

Command History

Use the Up and Down arrow keys to move through commands you have previously entered.

To display your command history:

Terminal window
history

This displays previously entered commands along with their history numbers.

TL;DR: The Bare Minimum1/2

Command Action
pwd Show where you are
ls Show what’s here
cd directory Change directory
mkdir name Create a directory
cat file Display a file

TL;DR: The Bare Minimum2/2

Command Action
vim file Edit a file using Vim
rm file Remove a file
sudo command Run a command with elevated privileges
Ctrl+C Stop a running command
Switch to normal mode

Contents