force_color_prompt=yes
${EDITOR:-emacs} #if varialbe $EDITOR exist, use it; otherwise set the default value as "emacs"
less -X #do not refresh the screen after less
/bin/rm or \rm #full path or '\' can escape the alias
HISTSIZE #the count of commands in history file
HISTFILESIZE #maxmium size of histfile
HISTIGNORE #commands in the list will not be recorded
HISTCONTROL #ignore lead space, reduplicative commands
HISTTIMEFORMAT #save time stamp in history. strftime(3)
/etc/inputrc #for shell which uses readline (bash,dash,zsh)
/etc/wgetrc,~/.wgetrc #set proxy fot wget
VARIABLE
assignment: 1. VAR=value 2. read VAR 3. VAR=`date`, VAR=$(date)
$0 command self
$# arguments counts
$* all parameters, list be treated as whole one
$@ all parameters, list be treated as every one
$? return code of last command
$! process ID
$$ shell's pid, usually used to create tempfile as name
shift remove variables
$PIPESTATUS last pipe return value
$SECONDS shell's run time,add 1 per second.in script, it is script's time
$RANDOM ramdom number
$BASHOPTS shopt list,same as 'shopt|grep -w on', 'shopt -s' to turn on, '-u' to turn off
checkwinsize -- update LINES and COLUMNS
cmdhist -- compress command to one line in history
hostcomplete -- complete command expand to hostname, act on /etc/hosts
$SHELLOPTS list of 'shopt -o'
'set -o' or '-e' to turn on, '+o' '+e' to turn off
errexit -- if command return not zero value, shell will exit
noglob -- pathname expand
monitor -- backgroud cmd finished, bash show a message before next prompt
pipefail -- substitute PIPESTATUS
'-x' -- when run a cmd, show it first, useful for debug (#!/bin/sh -x)
$BASH_COMMAND current execute cmd name
$BASH_SOURCE current file
$BASH_LINENO script current line number
$FUNCNAME current function name
FUNCNAME[0] -- current func
FUNCNAME[1] -- upper func
$HOSTNAME uname -n(more reliable, and can not be modificated by user)
$HOSTTYPE uname -m
$PWD, $OLDPWD pwd, cd -
$TIMEFORMAT 'time' command output format,%U represent user, %S system, %R total
$PPID parent shell's pid
$RANDOM random number 0~32767
$REPLY 'read' command's default variable
$BASH_XTRACEFD '-x' option, define output to which file(default stderr)
$GLOBIGNORE ingore wildchar pattern. GLOBIGNORE=*~(ignore backup file)
$IFS internal field separator:list the char used for space(defau:space,tab,newline)
$PATH '.' and '::' represent current dir,in PATH, the ":" on head or tail position will extend as current dir
$TMOUT act on 'read' 'select' or interaction shell, TMOUT seconds timeout
$TMPDIR default is /tmp
$GROUPS group id
${var}iable # usually, if include constant, string, filename, use uppercase; number, input, use lowercase
$((..)) , expr # math calculation, string instead by 0
${#variable} # variable's length
${parameter:offset[:length]} #cut string
${parameter: -offset[:length]} #cut from tail
${variable#word} #from head, not greedy matching
${variable##word} #greedy match, as long as possible
${variable%word} #from tail, not greedy
${var%%word} #greedy
${variable/old/new} #replace one
${variable//old/new} #replace all
${var/#old/new} #start with old
${var/%old/new} #end with old
${var/pattern} #pattern remove
${variable^^} #transpose to uppercase
${var,,} #to lowercase
${parameter:-word} # provide default value
${param:=word} # set default value for variable
${var?message} # if not be assigned, show message
${var+foo} # if has a value, foo will be calculated, if not return empty string
${!var} #use a var's name to visit another's value
WILDCHILD
[[:graph:]] A-Z,a-z,0-9 and punctuation
[[:alpha:]] A-Z,a-z
...
! and ^ at the head stand for don't include
set -o noglob (set -f) toggle off globbing('+' to toggle on)
shopt -s dotglob '*' matches dotfiles
shopt -s nullglob glob as null
failglob if not match, throw shell error instead deal with it
shopt -s extglob #
?(pattern), +(pattern), @(pattern)--match one ,!(pattern)--exclude,*()
shopt -s nocaseglob ignore case
CONDITION
[ is a program, ']' just the guy's parameter
-a -e # whether exist
-f # regular file
-b # block device
-c # character device
-L -h # symbolic link
-S # socket
-p # pipe
-d # directory
-r -w -x # permission
-O # owned by the effective uid
-G # gid
-g # suid
-u # sgid
-z # if length zero, true
-n # not zero, true
-s # exist and greater than zero
-N # last read, whether modified
A -ef B # if same inode of hard link
-nt # new than -- mtime
-ot # old than
= # same string
[[ "s1" < "s2" ]] #
[ != ] # not same
[[ =~ ]] # regex test. BASH_REMATCH[] contain matched expression
[ -eq ] # number equal
-ne # not equal
-lt # less than
-gt # greater than
-ge -le # not less, not greater
case statement
;;& #go match follow all pattern
;& #follow one pattern has been matched
LOOP
for foo in $var #has been know same opetation on a set, not until satisfied a condition,
('in $var' selectable, treated as $@
while #suitable for there is no list needed to traverse and need a condition to end the loop
#such as math, time compare, current process's outside status, continuing show option and read
user's input
: true # always true, return 0
read -n1 -p # read one char, show prompt
break n , continue n
select # execute loop, show prompt, set var to provided value, assign $REPLY with user input
useful for build simple menu quickly
FUNCTION and LIBRARY
func(), function foo, function foo() # three form, the first is universal
tail -F #output appended data as the file grows
pidof #find the pid of the named prog
trap #catch dignal
local #define local variable
/etc/security/limits.conf # nofile , maximum of open file
$OPTARG #option list
$OPTIND #index of current option
ARRAY
array=(foo bar baz qux)
read -a #read element to array
IFS=: read .. #only affect read
${#array[@]} or ${#array[*]} #the counts of array elements
${array[@]:m:n} #substr, from position m, forward n elements
declare -A array #relative array
${!array[@]} #reverse referance
+= #append elements
unset #delete elements
PROCESS
ps -fp<pid>
-ft<terminal>
-fu<user>
-F #more details
-eaf #all process
grep -w foobar | grep -v grep <=> grep -w foo[b]ar <=> pgrep
pgrep -l foo #pid and name
-u user
-x exact
-d #IFS
pidof foo #find by id, pgrep find by binary file name
/proc/self #running process
/proc/$$ #shell's pid
exec wait nohup
SHELL FAMILY
ksh
mksh, ksh93
csh
like C, ~,
tcsh
pushd,popd, locktty, correct spell,
zsh
more powerful
dash
small, quick
su - #login shell
su #not login, only interactive shell
---------------------------------------------------------
interactive login shell
bash # /etc/profile => ~/.bash_profile, ~/.bash_login, ~/.profile(as find first as read)
csh # /etc/csh.cshrc => /etc/csh.login, /etc/.login, /etc/login.std, /etc/cshrc(depend specify OS) => ~/.cshrc,
~/.login
sh # /etc/profile => ~/.frofile
tcsh # same as csh, if ~/.tcshrc exist, replace ~/.cshrc
ksh # /etc/profile => ~/.profile => /etc/ksh.kshrc => ~/.kshrc
zsh # /etc/zsh/zshenv => $ZDOTDIR/.zshenv => /etc/zsh/zprofile => $ZDOTDIR/.zprofile => /etc/zshrc =>
$ZDOTDIR/zshrc => /etc/zsh/slogin => $ZDOTDIR/.zlogin
---------------------------------------------------------
interactive nologin shell
bash # ~/.bashrc
csh # /etc/csh.cshrc, ~/.cshrc
sh # if $ENV, read it, else nothing
tcsh # /etc/csh.cshrc => ~/.tcshrc , ~/.cshrc
ksh # /etc/ksh.kshrc => ~/.kshrc
zsh # /etc/zsh/zhsenv => $ZDOTDIR/.zshenv =>/etc/zshrc , $ZDOTDIR/.zshrc
---------------------------------------------------------
non-interactive shell
bash # if $BASH_ENV,read it; else same as sh
csh # /etc/csh.cshrc => ~/.cshrc
sh # nothing
tcsh # /etc/csh.cshrc => ~/.tcshrc, ~/.cshrc
ksh #nothing
zsh # /etc/zsh/zshenv => $ZDOTDIR/.zshenv
---------------------------------------------------------
logout shell
zsh # $ZDOTDIR/.zlogout , /etc/zsh/zlogout
bash # ~/.bash_logout , /etc/bash_logout
csh # ~/.logout, /etc/.logout , /etc/logout, /etc/csh.logout
FILE OPERATION
stat -c, --format #"%a %U %G %n"
cat -n #line number
-s #suppress repeated empty output lines
-b #number nonempty lines
-v #show nonprinting
-T #display TAB as ^I
-e #display $ at end of each line
tac #concatenate and print files in reverse
/dev/zero # read from most fast
/dev/null # write in most fast
/dev/urandom # read from slowly like a snail
echo 3 > /proc/sys/vm/drop_caches # clear the system cache
mktemp --suffix .txt
join #join lines of two files on a common field
-jFIELD
sort -kFIELD
grep -i #ingore case
-w #match word
-v #reverse, list unmatched lines
-e #multiple pattern
-l #list file name
-A #after context
-B #before context
-C #context
split -b #specify size
-d #numberic
-a #suffix length
tee -a #append
touch -d #date format
find # use locate can be more fast, need run updatedb
-exec #run command
echo -e #backslash is special
-n #turn off append \n
^M #return
^H #/-\|
fmt -u #one space between two words, two space after .
-pSTR #reform prefix with STR
-s #split long line, if no this, the short line will be join
-t #indent first line diffirent with second line
-w #maxmium width, default 75
tail -f #output appended data as file grows
-F # -f --retry
-n0 #not report the existed content
od -a #named char
-x #hexdadecimal
-t #format type
paste -d #delimiter, -d, use csv form
-s #serial, paste one file at a time, transform column data to row data
pr -t #ignore title
-T #ignore page number
-COL #column
-J #join line, thiner
shuf -i # --input-range
-h # output at most count lines
--random-source #get random bytes from file
sort -u #delete repeated
-c #only check weather sorted
-t #specify separator instead of blank
-s #stabilize sort
-b #ignore leading blanks
-d #dictionary order, ignore punctuation
-f #ignore case
-g #general numberic sort
-i #ignore nonprinting
-h #human readable numbers(e.g.,2K 1G)
-M #month sort
-n #numberic sort
-R #sort by random hash of keys
-r #reverse the result of comparisons
-k #sort via a key
count the char of strings:echo one number line follow the string
tr -d #delete
uniq -u #only print unique lines
-d #only print duplicate lines
-w #compare no more than N characters in lines
-s N #skip chars
-f #skip fields
-i #ignore case
-c #count
wc -c #bytes
-w #words
-l #lines
getent #get entries from Name Service Switch libraries
groups #display current group name
logger #shell interface to syslog(3)
md5sum --check #read MD5 sums from the files
mkfifo #create named pipes
netcat -v
-z
-l
-p
-q
recipient
pv -t
-b
arp -a
ping -w #deadline
-c #count
-b #broadcast
scp -B #batch mode
ssh-keygen #authentication key generation
ssh-copy-id #append the public key to remote ~/.ssh/authorized_keys and set permission
-i #identity_file
ssh-agent #auth agent. if not GUI, eval `ssh-agent` will analyse the needed setting.
ssh-add #adds private key identities to the auth agent
ssh -n #redirect stdin from /dev/null(prevent reading from stdin)
-o #option. see also ssh_config
openssl #OpenSSL command line tool
genrsa #generation of DSA Private Key from param
req #PKCS#10 X.509 Certificate Signing Request Management
s_client #SSL/TLS client program
s_server #SSL/TLS server program
nohup #run a command immune to hangups
seq
sleep
timeout -s #signal
-k #send a SIGKILL
uname #kernel base name
-n #hostname
-r #kernel realease
-m #cpu architecture
-o #os
-i #hardware plateform
uuencode
xargs -0 #Input items are terminated by a null character
-L #max-lines
-I #filename replace str
yes
ps -o comm #header and process name
-o comm= #no header