Bash Shell

From Leo's Notes
Revision as of 02:26, 1 September 2019 by Leo (talk | contribs) (formatting)
This page was last edited on 1 September 2019, at 02:26.

Bash, short for the Bourne Again Shell, is a popular interactive shell for Unix based systems and is the default for many distributions.

This article will go over some tips and tricks on using Bash interactively. For more information on scripting in Bash, see Bash Scripting.

User Interactive Keys

Here are some keyboard shortcuts when using the interactive shell. These also work under zsh.

Navigation

CTRL + a Go to start of line (Home key)
CTRL + e Go to end of line (End key)
CTRL + b Back one character (left arrow)
CTRL + f Forward one character (right arrow)
ALT + b Back one word (like in vi 'b')
ALT + f Forward one word (like in vi 'w')

Editing

CTRL + d Delete character under cursor (Delete key)
CTRL + h Delete character before cursor (backspace)
ALT - U Converts to uppercase the word ahead of the cursor
ALT - L Converts to lowercase the word ahead of the cursor
CTRL + w Cuts word before cursor to clipboard
CTRL + u Cuts entire line to clipboard
CTRL + y Pastes the contents of the clipboard (un yank)
CTRL + - Undo a change
ALT + r Restores line from history

Previous Command / Arguments

!! Run last command
!xyz Run last command starting with xyz
!* All arguments of the previous command
!$ / ALT + . Last argument of the previous command
!!:p / !xyz:p / !*:p / !$:p Prints instead of executes the command/arguments
^abc^xyz Runs the previous command with 'abc' replaced with 'xyz'

Others

CTRL + l Clears screen
CTRL + q Stops flow / pauses output
CTRL + s Resumes flow / unpauses output

Redirection

2>&1 STDERR to STDOUT
&> Redirect both STDERR and STDOUT

If you wish to redirect both STDERR and STDOUT, you need to redirect STDOUT and then STDERR like so:

# ./some-program > /dev/null 2>&1

Prompt

The shell prompt can be set by changing the PS1 environment variable.

See Also