Bash Shell Keyboard Shortcuts

Bash Shell Keyboard Shortcuts
  • The Bash (Bourne Again SHell) utilizes the Readline library, providing a variety of efficient keyboard shortcuts to facilitate improved command line interaction and editing.

Control Shortcuts

  • Control C (Interrupt)

  • Effect: Sends the SIGINTSIGINT signal to terminate the current foreground process.

  • Use Case: Effectively stops a command that is hung or executing indefinitely (e.g., ping or sleep).

  • Control D (End of File)

  • Effect: Sends an End-of-File (EOF) marker to the shell.

  • Use Case: Useful for exiting the current terminal session or terminating input in interactive commands without a specific exit keyword.

  • Control A

  • Effect: Moves the cursor instantly to the start of the current command line.

  • Use Case: Allows for quick editing at the beginning of a long command.

  • Control E

  • Effect: Moves the cursor instantly to the end of the current command line.

  • Use Case: Quickly append arguments or flags to a command.

  • Control R (Reverse Search)

  • Effect: Initiates a reverse-i-search through the shell history.

  • Use Case: Type any part of a previous command to locate it; press Control R again to cycle through older matches.

  • Control L

  • Effect: Clears the terminal screen, equivalent to the clear command.

  • Use Case: To remove visual clutter while keeping the current line active at the top.

  • Control U

  • Effect: Deletes the entire line from the cursor back to the beginning.

  • Control K

  • Effect: Deletes the rest of the line from the cursor position to the end.

  • Control W

  • Effect: Deletes the word immediately preceding the cursor.

  • Control Y (Yank)

  • Effect: Pastes the text most recently removed by Control U, Control K, or Control W.

  • Tab

  • Effect: Command and file name auto-completion.

  • Use Case: Reduces typing and prevents syntax errors by completing partially typed names.

Alt Shortcuts

  • Alt B

  • Effect: Moves the cursor backward by one word.

  • Alt F

  • Effect: Moves the cursor forward by one word.

  • Alt T

  • Effect: Swaps the word at the cursor with the preceding word.

  • Alt U / Alt L

  • Effect: Converts the current word starting from the cursor to uppercase (U) or lowercase (L).

History Expansion (Bang Commands)

  • !! (Bang Bang)

  • Effect: Reprints and executes the last command entered.

  • Common Usage: sudo !! used when a command fails due to missing superuser privileges.

  • !$

  • Effect: Represents the last argument of the previous command.

  • Use Case: If you run mkdir test_folder, follow up with cd !$ to immediately enter it.

Practical Demonstrations

  1. Superuser Permissions

  • Command: useradd Linda

  • Scenario: You realize you forgot sudo.

  • Action: Use Control A to jump to the start and type sudo, or simply run sudo !!.

  1. Editing Long Paths

  • Command: ls /usr/local/nginx/conf/conf.d/

  • Action: Use Alt B to jump word-by-word through the path to fix a typo in nginx.

  1. Process Termination

  • Command: sleep infinity

  • Action: Use Control C to kill the idle process and return to the prompt.

  1. Text Manipulation

  • Command: sudo useradd bob

  • Change: You decide to use a different name. Use Control W to delete "bob" and type the new name, or use Control U to clear and start over.

  1. Screen Cleanup

  • Action: Use Control L to refresh the workspace after multiple command outputs.