Bash

Every time a shell is started, three files are automatically opened for your use. These files are called stdin, stdout, and stderr.

lsof +f g -ap $BASHPID -d 0,1,2

I/O Redirection

  • Redirecting stderr and stdout to a file: command &> file or command > file 2>&1
  • Redirecting stderr and stdout to separate files: command > file1 2> file2
  • Redirect stderr and stdout to /dev/null: command > /dev/null 2>&1 or command &> /dev/null

Customize prompt

vi .bashrc

export PS1="[\[\e[1;33m\]\u@\[\e[1;31m\]\h\[\e[0m\] \W]\\$ "

Color table

Black 0;30
Red 0;31
Green 0;32
Yellow 0;33
Blue 0;34
Magenta 0;35
Cyan 0;36
White 0;37

1 = Bold

Clear history file

history -c > .bash_history

Script collection

vi .bash_profile

PATH=$PATH:$HOME/bin

Better bash history

export HISTTIMEFORMAT='%Y-%m-%d %H:%M:%S - '
shopt -s histappend
HISTSIZE=100000
HISTFILESIZE=100000
HISTCONTROL=ignoreboth
HISTIGNORE='ls:bg:fg:history'

Debug

bash -x test.sh 2>&1 | tee out.test

bash -n test.sh

Commands

  • Display current flags: echo $-
  • Print terminal line settings (keystrokes are processed by stty before the shell): stty -a
  • Display a full list of all currently bound and available keybindings: bind -p
  • Display only the binding that use the Control key: bind -p | grep '\\C'
  • Display the binding for Ctrl + a: bind -p | grep '\\C-a'
  • Install Bash completion: yum install bash-completion
  • Display current prompt setting: echo $PS1
  • Display all shell options: shopt
  • Display all enabled shell options: shopt -s
  • Display all disabled shell options: shopt -u
  • Lists all variables (local & environment variables): set
  • List only environment variables: export or env
  • Run a command without logging: uptime;history -d $(history 1)
  • Run all commands without logging: unset HISTFILE

Bash history

mkdir /root/.history
vi /root/.bash_profile
export HISTFILE=~/.history/histfile.$               # per session
export HISTFILE=~/.history/`date +%Y%m%d`.hist      # per day
export HISTFILE=~/.history/`date +%Y-%W`.hist       # per week
export HISTTIMEFORMAT="%h/%d - %H:%M:%S "           # date + time

Shortcuts

CTRL Key Bound

| Ctrl + a   | Jump to the start of the line                      |
| Ctrl + b   | Move back a char                                   |
| Ctrl + c   | Terminate the command                              |
| Ctrl + d   | Delete from under the cursor                       |
| Ctrl + e   | Jump to the end of the line                        |
| Ctrl + f   | Move forward a char                                |
| Ctrl + k   | Delete to EOL                                      |
| Ctrl + l   | Clear the screen                                   |
| Ctrl + p   | Cycle to previous command                          |
| Ctrl + n   | Cycle to next command                              |
| Ctrl + r   | Reverse index search                               |
| Ctrl + R   | Search the history backwards with multi occurrence |
| Ctrl + u   | Delete backward from cursor                        |
| Ctrl + xx  | Move between EOL and current cursor position       |
| Ctrl + x @ | Show possible hostname completions                 |
| Ctrl + z   | Suspend/ Stop the command                          |
| Ctrl + y   | Paste text that was cut using ctrl-u               |
| Ctrl + w   | Delete the word on the left of the cursor          |
| Ctrl + h   | Delete char before cursor (think backspace)        |
| Ctrl + j   | Execute current command (same as Enter/Return)     |

Alt Key Bound

| Alt + <         | Move to the first line in the history        |
| Alt + >         | Move to the last line in the history         |
| Alt + ?         | Show current completion list                 |
| Alt + *         | Insert all possible completions              |
| Alt + /         | Attempt to complete filename                 |
| Alt + .         | Yank last argument to previous command       |
| Alt + b         | Move backward                                |
| Alt + c         | Capitalize the word                          |
| Alt + d         | Delete word                                  |
| Alt + f         | Move forward                                 |
| Alt + l         | Make word lowercase                          |
| Alt + n         | Search the history forwards non-incremental  |
| Alt + p         | Search the history backwards non-incremental |
| Alt + r         | Recall command                               |
| Alt + t         | Move words around                            |
| Alt + u         | Make word uppercase                          |
| Alt + backspace | Delete backward from cursor                  |

Twice TAB

| 2T         | All available commands(common)                  |
| (string)2T | All available commands starting with (string)   |
| /2T        | Entire directory structure including Hidden one |
| 2T         | Only Sub Dirs inside including Hidden one       |
| *2T        | Only Sub Dirs inside without Hidden one         |
| ~2T        | All Present Users on system from "/etc/passwd"  |
| $2T        | All Sys variables                               |
| @2T        | Entries from "/etc/hosts"                       |
| 2T         | Output like ls or dir                           |

Links