Tricks for entering Bash shell commands in the terminal

Disclaimer: These work in my current setup using iTerm2 on macOS. Other environments may vary.

Keyboard shortcuts when typing at the command line

Tab completion

  • Tab to complete it with the unique match in the file system
  • double-Tab to display a list of completion options if there are multiple matches

Navigating within the line

  • Ctrl+E to jump to the end of the line
  • Alt+← to jump left by word
  • Alt+→ to jump right by word
  • Option+click to move the cursor with the mouse


  • Ctrl+W deletes left by a word
  • Ctrl+K deletes (“kills”) the rest of the line
  • Ctrl+U deletes the line up to the cursor

Accessing command history

  • Ctrl+R to reverse-search commands by typing
    Enter Ctrl+R, then type part of the command that you want to retrieve. If there are multiple matches, keep typing or enter Ctrl+R until the one you want displays.
  • The recent bash command history is stored in the file ~/.bash_history

Some of the above commands will be familiar to Emacs users. Another one: Ctrl+Y pastes (“yanks”) material previously deleted by Ctrl+W, Ctrl+K, or Ctrl+U.

More keyboard shortcuts here.

Path shortcuts

  • . stands for the current working directory (where you currently are in the file system when executing a command)
  • .. stands for the parent directory
  • ~ or $HOME stands for your user home directory

Variables and aliases

x='a long string'y=./$x.txtecho $y

will print

./a long string.txt

Thus if you need to reuse a string in a bunch of commands in your current session, this can save you time.

To set an environment variable that can be accessed by programs you run at the command line in your current session, precede the variable name with the word export. This is usually used for special variables like PATH.

The alias command can be used to create a custom shortcut for frequent commands: e.g.

alias ll='ls -l'

will mean that the ll command acts as a shortcut for ls -l for the remainder of the session.

Storing variables/aliases across sessions

Some of the contents of my ~/.bash_profile script:

alias ls='ls -G'
alias ll='ls -l'
alias lsa='ls -A'
export MYSQL="/usr/local/mysql"
export PATH=${MYSQL}/bin:$PATH

The first 3 are aliases for variants of the ls command. The last 2 are environment variables that help Bash find executables for the MySQL installation.

If you modify ~/.bash_profile and want to import it into the current session, use the source command:

source ~/.bash_profile

Thanks to Michael Kranzlein for providing feedback on a draft of this post.

Assistant Professor of Linguistics and Computer Science, Georgetown University ▪

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store