Shell 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, hostname, etc.)
The Bash prompt is customizable. By default, it gives the username and the hostname, and it ends with the dollar sign ($
).
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
To execute a command, you type it, then press the <enter>
.
Your turn:
Run your first command:
ls
Command options
Commands come with a number of flags (options).
Some flags are short (a single hyphen followed by a single letter), others are long (two hyphens followed by a word or a series of words separated by hyphens). Some flags have both a long and short forms (in which case, both are totally equivalent).
Examples of flags for the ls
command:
- List all files and directories (not ignoring hidden files):
ls -a
orls --all
- 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
orls --human-readable
Short flags can be combined and the flag order doesn’t matter, so the followings are all equivalent:
ls -alh
ls -a -l -h
ls -ahl
ls -l -ha
ls --human-readable -al
ls --all --human-readable -l
- …
Commands documentation
Man pages
The manual page for a command can be accessed with the command man
:
man <command>
The <
and >
symbols are used to delineate a generic placeholder that you should replace by the value of your choice (here, for instance, man ls
).
Man pages open in a pager (usually less).
Useful keybindings when you are in the pager:
SPACE scroll one screen down
b back one screen
q quit the pager
g go to the top of the document
7g go to line 7 from the top
G go to the bottom of the document
/ search for a term
n will take you to the next result
N to the previous result
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 on commands
Help for commands can be printed to the standard output (the terminal) with:
<command> --help
Your turn:
Print the help for the ls
command in your terminal.
Type of commands
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 either of:
command -V <command>
type <command>
Your turn:
What is the nature of the pwd
command?
Shell keybindings
Here are useful keybindings that you can use in the shell (they all come from the text editor Emacs):
tab auto-complete command
C-l clear the terminal
C-p navigate the command history backward
C-n navigate the command history forward
C-a go to the beginning of the line
C-e go to the end of the line
C-k delete to the end of the line
C-u delete to the beginning of the line
C-f go forward one character
C-b go backward one character
M-f go forward one word
M-b go backward one word
C-l
means: press the Ctrl (Windows) or Command key ⌘ (macOS) and l keys at the same time.
M-f
means: press the Alt (Windows) or Option (macOS) and f keys at the same time.