Bash: the basics

Author

Marie-Hélène Burle

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

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).

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 equivalent:

  • ls -alh
  • ls -a -l -h
  • ls -ahl
  • ls -l -ha

Help on commands

The command man provides an interface to the system reference manual.

To access the manual page of a command, you type:

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          backa 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 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 either of:

command -V <command>
type <command>

Your turn:

What is the nature of the pwd command?

Shell keybindings

Here are a few useful keybindings that you can use in the shell:

  • Clear the terminal (command clear) with C-l (this means: press the Ctrl and l keys at the same time).
  • Navigate the 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 right of the symbol # is ignored by the interpreter and is for human consumption only.

# You can write full-line comments

pwd       # You can also write comments after a command

Comments are used to document scripts (text files with a number of commands). DO USE THEM: future you will thank you 🙂.