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/DocumentsA relative path starts from your current directory:
Documents/example.txtPaths2/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:
pwdListing Directory Contents
To see the files and directories inside it:
lsTo include hidden files:
ls -aMoving to Different Directories
Use cd to move to a different directory:
cd directoryFor example:
cd DocumentsCreating Directories
Create a directory with mkdir:
mkdir exampleRemoving Directories
Remove an empty directory:
rmdir exampleFiles
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
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 Moving1/2
Copy a file:
cp source destinationFor example:
cp example.txt backup.txtCopying and Moving2/2
Move a file:
mv example.txt Documents/mv can also be used to rename a file:
mv example.txt renamed.txtRemoving Files
Remove a file:
rm example.txtRunning Commands
Running Commands as Administrator1/2
Some system changes require administrator privileges. Use sudo before a command to run it with elevated privileges:
sudo commandFor example:
sudo apt updateRunning Commands as Administrator2/2
You may be asked for your password.
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
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:
cd DocTab Completion2/2
Then 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
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 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 |
Up next
CompTIA Network+