Intro to Bash
Author(s): Matthias Lee (ml2322)
Last Updated: 07-02-2025
Details
Recommended Prerequisites (click to expand)
NoneIn most Linux operating systems, the terminal runs a program called bash. This is the command processor, where you interact
with the system. Some use other ones like sh, zsh, fish, or dash. However, since they are mostly similar, this guide will still apply.
What am I looking at?
When you load the terminal, you are greeted with a prompt like this:
user@system:/home/user$
This tells you who you are, the system name, the current path, and your privileges.
user: Your usernamesystem: The computer's hostname/home/user: The current working directory (where you are)$: A$at the end signifies you are operating with regular privileges. If you are the root user, however, you will see a#instead (don't worry about this for now).
Navigating
Using terminal in the same directory isn't very practical, so learning to navigate to different directories is important.
Checking your current directory
To see your current directory, you can run pwd (print working directory), and it will print your full working directory.
user@system:/home/user$ pwd
/home/user
Changing directories
To change your directory, run the cd command, followed by where you want to go. If you run cd with no arguments, you will go to your home
directory.
user@system:/something/else$ cd
user@system:/home/user$
user@system:/home/user$ cd /home/user/Desktop
user@system:/home/user/Desktop$
Absolute vs relative paths
Absolute paths are paths that start with /, meaning they start at the root directory of the system and specify the full path to get
to the desired file. This is useful for hardcoding anything, or using something in a completely different set of directories as where
you are now.
However, when running commands or changing directories, it's not always necessary to specify the full path from the root directory (/).
Often, you can specify just a relative path, which is the path to a file or folder from where you are now. For example, if you
are in /home/user and want to get to /home/user/Desktop, you don't actually have to specify that full path, you can just run
cd Desktop. Or, if you want to access a subfolder in Desktop, you can just do cd Desktop/subfolder.
user@system:/home/user$ cd Desktop
user@system:/home/user/Desktop$
user@system:/home/user/Desktop$ cd subfolder/important_files
user@system:/home/user/Desktop/subfolder/important_files$
Additionally, if you need to go back a directory, you can use the .. shortcut. .. means parent directory, which means you can
cd to it to go back, or use it in a relative path to go somewhere else.
user@system:/home/user/Desktop/subfolder$ cd ..
user@system:/home/user/Desktop$ cd ../Documents
user@system:/home/user/Documents
Another handy trick is that ~ at the start of the path signifies your home directory. For example, you can cd ~ to go to the home
directory, or you can specify ~/Desktop/file.txt or something similar in a command instead of needing to write /home/user/Desktop/file.txt
Passing arguments
When you run a command, you can pass arguments in it to make it do specific things. Arguments are different per command, but there are general rules for how you have to pass them.
Spaces
Arguments are separated by a whitespace (" " or a new line). For example: a_command arg_1 arg_2. If you need to pass a space in the argument,
however, you have two options. You can escape the space by preceding it by a backslash (arg with space becomes arg\ with\ space), or you can
put the whole thing in quotes ("arg with space")
Special characters
Certain characters have special meanings in bash, such as $, \, {, }, and some others. If you need to use these characters, you can escape
them by preceding them with a backslash, just like with spaces. Example: ($a does not work as expected, but \$a does).
Flags
Many commands don't just take arbitrary input; they use flags to modify behavior. Flags are arguments that usually look like -a or --example.
Usually, you can find information on what flags exist and what they do by specifying --help.
Basic commands
Here are some basic commands you can use in almost all Linux systems:
ls <dir>: This command lists everything in the directory you specify (<dir>). If you don't specify one, it will list everything in your current directory.cat <files>: This command prints the content of every file you specify. You can specify several files, and it will concatenate them and print the result.echo <text>: This just prints out whatever text you specify. Echo is unique in that if you specify several arguments, it will just print everything you specify.mv <old_file> <new_file>: Themvcommand moves or renames the first file (<old_file>) to the new file (<new_file>). If the new file is the path to a directory, it will keep the file name but put it in that directory. Otherwise, it will rename it to whatever you set.rm <file>: Thermcommand deletes the file specified. It allows the-rand-fflags, which remove recursively (for deleting directories) and remove forcefully (for write protected files) respectively.gedit <file>: This command opens the specified file ingedit, the default GUI text editor for Ubuntu. Some other distros may have a different editor, so this may not always work.whoami: This will just return your username.
Some other tips
- You can use the up and down arrow keys to navigate through your recent commands; to run your last command again, just hit the up arrow and it will come back.
CTRL+AandCTRL+Emove the cursor to the beginning and end of the command respectively. This is useful if you don't have Home and End on your keyboard.- You can hit Tab to autocomplete a command or file path so you don't have to type the whole thing.