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[edit]

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

Navigation[edit]

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[edit]

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[edit]

!! 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[edit]

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

Redirection[edit]

Standard Out and Standard Error represents a file descriptor on the system. Messages are typically sent to standard out for the user to see while errors are sent to standard error so that users can see errors even if a command's output is piped elsewhere.

Standard out and standard error are just file descriptors the kernel sets up on start up and always have file descriptor 1 and 2 respectively. You can verify this by listing the two devices in /dev.

$ ls -al /dev/std*
lrwxrwxrwx 1 root root 15 Sep 10 13:32 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 Sep 10 13:32 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 Sep 10 13:32 /dev/stdout -> /proc/self/fd/1

In a shell, you can redirect these standard messages to another file descriptor with the > operator. You reference another file descriptor with the & followed by the file descriptor number. Applying this, you could:

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 exactly like this:

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

The ordering of the redirection matters.

Prompt[edit]

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

Disable XOFF Key Binding[edit]

XOFF will stop all transmission from your console to your terminal and by default is bound to Ctrl+S key. When using programs that require key bindings to CTRL+S and CTRL+Q, this may be problematic as your console may stop responding.

To disable XOFF completely, add this your .bashrc file:

stty ixany
stty ixoff -ixon
stty stop undef
stty start undef

See Also[edit]

Enable Dark Mode!