Bash Shell

From Leo's Notes
Revision as of 19:54, 24 May 2019 by Leo (talk | contribs)
This page was last edited on 24 May 2019, at 19:54.

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

Test

-e file exists
-a file exists (deprecated)

This is identical in effect to -e

-f file is a regular file (not a directory or device file)
-s file is not zero size
-d file is a directory
-b file is a block device. Eg. /dev/sda.
-c file is a character device. Eg. /dev/tty0.
-p file is a pipe. Eg. /dev/fd/0.
-h file is a symbolic link
-L file is a symbolic link
-S file is a socket
-t file (descriptor) is associated with a terminal device

This test option may be used to check whether the stdin [ -t 0 ] or stdout [ -t 1 ] in a given script is a terminal.

-r file has read permission (for the user running the test)
-w file has write permission (for the user running the test)
-x file has execute permission (for the user running the test)
-g file or directory has set-group-id (sgid) flag set

If a directory has the sgid flag set, then a file created within that directory belongs to the group that owns the directory.

-u file has set-user-id (suid) flag set
-k file or directory has sticky bit set
  • If set on a file, that file will be kept in cache memory, for quicker access.
  • If set on a directory, it restricts write permission. Setting the sticky bit adds a t to the permissions on the file or directory listing. This restricts altering or deleting specific files in that directory to the owner of those files.
-O you are owner of file
-G group-id of file same as yours
-N file modified since it was last read
f1 -nt f2 file f1 is newer than f2
f1 -ot f2 file f1 is older than f2
f1 -ef f2 files f1 and f2 are hard links to the same file
! "not" -- reverses the sense of the tests above (returns true if condition absent).


Prompt

The shell prompt can be set by changing the PS1 environment variable. This is my colored prompt:

<phorkie>https://phorkie.leo.home.steamr.com/12/embed</phorkie>

See Also