Bash (the "Bourne Again Shell") reads a line of input, splits it into command and arguments, and either runs a built-in behavior or looks up an executable on $PATH - see Environment Variables for how PATH and similar variables work.

Anatomy of a command

grep -i "error" /var/log/syslog
  • grep - the command
  • -i - a flag (case-insensitive matching)
  • "error" - an argument, quoted because it could contain shell-special characters
  • /var/log/syslog - a second argument, here a file path

Flags can usually be combined (-il instead of -i -l) and most commands support a long form (--ignore-case instead of -i) - check man grep or grep --help for a given command's options rather than guessing.

Navigation

pwd                 # print working directory
cd /var/log         # change directory
cd ..                # up one level
cd -                 # back to the previous directory
cd                    # home directory (equivalent to `cd ~`)
ls -la                # list all files, long format, including hidden ones

Command history and editing

  • / - cycle through previous commands
  • Ctrl+R - search history interactively; type to filter, Ctrl+R again to cycle matches
  • Ctrl+A / Ctrl+E - jump to start / end of the line
  • !! - re-run the last command (sudo !! is the classic use: re-run the previous command with sudo after forgetting it)

Tab completion

Pressing Tab completes file paths, command names, and (with the bash-completion package installed, standard on Debian/Ubuntu, optional on some minimal installs) many commands' own arguments and flags. Press Tab twice to list all possibilities when completion is ambiguous.

Aliases and shell configuration

Bash reads ~/.bashrc for interactive non-login shells (most terminal sessions) and ~/.bash_profile or ~/.profile for login shells - the practical takeaway is: put aliases and interactive tweaks in ~/.bashrc, and reload changes without restarting the terminal with:

source ~/.bashrc
alias ll='ls -la'
alias gs='git status'

Where this ends and scripting begins

Everything above works the same whether typed interactively or saved into a file - see Shell Scripting for turning sequences of commands into reusable scripts, and Pipes and Redirection for connecting commands together instead of running them one at a time.