Bash: the basics
What does it feel like to work in a shell? Here is a first basic orientation.
The prompt
In command-line interfaces, a command prompt is a sequence of characters indicating that the interpreter is ready to accept input. It can also provide some information (e.g. time, error types, username and hostname, etc.)
The Bash prompt is customizable. By default, it often gives the username and the hostname, and it typically ends with $
.
Commands
Bash comes with a number of commands: directives to the shell to perform particular tasks.
Examples of commands:
- Print working directory:
pwd
- Change directory:
cd
- Print:
echo
- Print content of a file:
cat
- List files and directories in working directory:
ls
- Copy:
cp
- Move or rename:
mv
- Create a new directory:
mkdir
- Create a new file:
touch
Command options
Commands come with a number of flags (options).
Examples of flags for the ls
command:
- List all files and directories (not ignoring hidden files):
ls -a
- List files and directories in a long format:
ls -l
- List files and directories in a human readable format (using units such as K, M, G):
ls -h
Flags can be combined. The order doesn’t matter and the followings are all the same:
ls -alh
ls -a -l -h
ls -ahl
ls -l -ha
- …
Help on commands
The command man
provides an interface to the system reference manuals.
To access the manual page of a command, you thus can type:
man <command>
Man pages open in a pager (usually less). Here are some usual key bindings to navigate the pager:
- You can scroll down with the space bar and back up with the
b
(for “back”) key. - Go to the top of the document with the
g
(for “go”) key.G
will send you to the end of the document and7g
will send you to line7
(of course, this works with any number). - To search for a term, press
/
followed by the term, then enter. You can then find subsequent/previous results with then
(for “next”) andN
keys. - Quit the pager with the
q
(for “quit”) key.
Your turn:
- Open the man page for the
ls
command. - Navigate down a few pages, then navigate back up.
- Search for the first 5 occurrences of the word “directory”.
- What does
ls -r
do? - Finally, leave the pager.
Help pages can be accessed with:
<command> --help
Your turn:
Access the help of the ls
command.
To know the nature of a command (e.g. shell built-in function, an alias that you created, or the path of an utility) run:
command -V <command>
Your turn:
What is the nature of the pwd
command?
Keybindings
Clear the terminal (command clear
) with C-l
(this means: press the Ctrl
and L
keys at the same time).
Navigate command history with C-p
and C-n
(or up and down arrows).
You can auto-complete commands by pressing the tab
key.
Comments
Anything to the left of
#
is ignored by the interpreter and is for human consumption only.Comments are usually used in scripts to document what sections of the code do or add information for the reader of the script (which is often future you, so commenting your scripts will help you a lot later on).