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
Section titled “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.
A path describes the location of a file or directory.
An absolute path starts from the root directory /:
/home/username/DocumentsA relative path starts from your current directory:
Documents/example.txtThe following have special meanings:
| Path | Meaning |
|---|---|
. |
Current directory |
.. |
Parent directory (one directory above your current directory) |
~ |
Your home directory |
/ |
Root directory |
Directories (Folders)
Section titled “Directories (Folders)”Current Directory
Section titled “Current Directory”Your current directory is the directory you are currently working in.
To see your current directory:
pwdListing Directory Contents
Section titled “Listing Directory Contents”To see the files and directories inside it:
lsTo include hidden files:
ls -aMoving to Different Directories
Section titled “Moving to Different Directories”Use cd to move to a different directory:
cd directoryFor example:
cd DocumentsCreating Directories
Section titled “Creating Directories”Create a directory with mkdir:
mkdir exampleRemoving Directories
Section titled “Removing Directories”Remove an empty directory:
rmdir exampleViewing a File
Section titled “Viewing a File”Display the contents of a text file:
cat example.txtFor longer files, you can use:
less example.txtUse the arrow keys to move through the file and press q to quit.
Editing a File
Section titled “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:
vim example.txtSee the Vim reference for the commands needed to edit, save, and quit.
Copying and Moving
Section titled “Copying and Moving”Copy a file:
cp source destinationFor example:
cp example.txt backup.txtMove a file:
mv example.txt Documents/mv can also be used to rename a file:
mv example.txt renamed.txtRemoving Files
Section titled “Removing Files”Remove a file:
rm example.txtRunning Commands
Section titled “Running Commands”Running Commands as Administrator
Section titled “Running Commands as Administrator”Some system changes require administrator privileges. Use sudo before a command to run it with elevated privileges:
sudo commandFor example:
sudo apt updateYou may be asked for your password.
Stopping a Command
Section titled “Stopping a Command”To interrupt a command that is currently running:
Ctrl+CThis is useful when a command is hanging or when you need to stop a running program.
Making Your Life Easier
Section titled “Making Your Life Easier”Tab Completion
Section titled “Tab Completion”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:
cd DocThen press Tab to complete it:
cd Documents/If there is more than one possible match, press Tab again to display the available options.
Command History
Section titled “Command History”Use the Up and Down arrow keys to move through commands you have previously entered.
To display your command history:
historyThis displays previously entered commands along with their history numbers.
TL;DR: The Bare Minimum
Section titled “TL;DR: The Bare Minimum”| 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 |
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 |