- Print the current shell version and process id :
ps -p $$ and therefore to check the version of current shell, use something like ps -p $$ | grep -q 'bash'
- The variable holding the current program name :
$0
- The current shell:
$SHELL
- Replace some content within a file :
sed 's/target/replacement/g' target_file_path
- Print the folder data structure:
- Check the source of a symbol link :
readlink
- Check the disk usage and directory sizes:
- Disk statistics:
df -h
- Directory size:
du -hs /path/to/directory
- Launch the script in the current shell:
. ./shell_script.sh
-
find : walk a file hierarchy
find /path/to/directory -type f -print0: prints the path to all the file within the directory to the standard output
find directory_path -name "fileName" -print0: find all the files with the name matches "fileName" and print the file path to standard output
-
Exit code: decides the execution status of the bash command.
$? stores the exit status of the last foreground command. Need to use a variable to hold it's value in the bash scripts
- Note here 0 is considered to be true so
&& will only operate the next command if returns a 0 in the previous command
- Grouping the statements: use curly braces to group the statements to make them as a union and therefore to change the execution order of the bash.
-
/dev/null: the null device is a device file that discards all data written to it but reports that the write operation succeeded
- And to redirect error msg(0 for stdin, 1 for stdout, 2 for err) to it :
2>/dev/null
2>&1: redirects stderr to the stdout's place( note 1 is stdout ). So if stdout has been redirected to /dev/null, then the stderr will go there as well
- Print the strings with the whitespace marks clearly printed :
printf %q "string"
-
read: normally read will read the stdin, but we can use -u to let it read from another file descriptor.
- Use exec to open the file :
exec 3 < input.txt; And then read by : read -u 3 line Or read line < &3
- And to close file descriptor:
exec n > &- ( this closes the output file descriptor, so in order to close the input file descriptor, change the direction correspondingly.)
-
Some interesting commands of
[
-z STRING: True if the string is empty (it's length is zero).
-n STRING: True if the string is not empty (it's length is not zero).
EXPR -a EXPR: True if both expressions are true (logical AND).
EXPR -o EXPR: True if either expression is true (logical OR).
-
Some interesting commands of
[[
STRING = (or ==) PATTERN: Not string comparison like with [ (or test), but pattern matching is performed. True if the string matches the glob pattern.
STRING =~ REGEX: True if the string matches the regex pattern.
( EXPR ): Parentheses can be used to change the evaluation precedence.
EXPR && EXPR: Much like the '-a' operator of test, but does not evaluate the second expression if the first already turns out to be false.
EXPR || EXPR: Much like the '-o' operator of test, but does not evaluate the second expression if the first already turns out to be true.
-
The Braces{}:
- Truncate the contents of a variable:
$ var="abcde"; echo ${var%d*} output: abc
- Make substitutions similar to sed:
$ var="abcde"; echo ${var/de/12} output: abc12
-
Use a default value:
$ default="hello"; unset var; echo ${var:-$default} output: hello
- Substitution for the expression only
${parameter:-word}if parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
- Substitution for the expression and change the variable
${parameter:=word}If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way.
${parameter:?word}If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.
- Inverse logic--substitute when the variable has value
${parameter:+word}If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.
-
brace expansion: (note as a expansion operator, of course there shouldn't be any $ within the braces. So file name and the commands only!!)
echo f{oo,ee,a}d; output : food feed fad
mv error.log{,.OLD}; output : mv error.log error.log.OLD
for num in {000..2}; do echo "$num"; done; output : for num in {000..2}; do echo "$num"; done
echo {00..8..2}; output : 00 02 04 06 08
-
大小写变换: ^大写,,小写, ~大小写切换,重复一次只修改首字母,重复两次则应用于所有字母。
HI=HellO; echo "$HI" # HellO; echo ${HI^} # HellO; echo ${HI^^} # HELLO
-
移除匹配的字符串
- eg:
FILENAME=/home/spacewander/param.sh
- %xx 从后往前,开始匹配,移除匹配的内容
echo ${FILENAME%/*} # /home/spacewander
- %%xx 跟上面的差不多,不过这是贪婪匹配
echo ${FILENAME%%/*} #
- #xx 从前往后,开始匹配,移除匹配的内容
echo ${FILENAME#*/} # home/spacewander/param.sh
- ##xx 跟上面的差不多,不过这是贪婪匹配
echo ${FILENAME##*/} # param.sh
-
查找并替换
- /MATCH/VALUE 替换第一个匹配的内容
echo ${FILENAME/home/office} # /office/spacewander/param.sh
- //MATCH/VALUE 替换匹配的内容
echo ${FILENAME//s/S} # /home/Spacewander/param.Sh
-
Indirect Reference of Variable Names
- In bash :
${!second_name}
- In zsh :
${(P)second_name}
- Actually what we want to do here is to ask the shell to process the expression twice( the first time is to expand the variable containing the real variable name. the second is to expand the real variable) So we can use
eval here. Eg : eval echo /$$a Note eval is to process the command with shell twice
-
The parentheses():
-
Subshell: single parentheses create a subshell to run the code without affecting the current environment
Mainly for getting the output of a complex command. Similar to ` ` here. Eg: echo $(seq 1 10)
-
arithmetic operations: basically the double parentheses behaves very much likely to the
let but have more freedom in using the spaces.
((a++))
((meaning = 42))
for ((i=0; i<10; i++))
echo $((a + b + (14 * c)))
- Array constructor:
array=(1 2 3). And to check the total number of elements in the array : echo ${#arary}
-
The brackets[]:
- The old test function
- Can be used for array indices::
array=(1 2 3); echo array[1]
- BashGuide/TestsAndConditionals - Greg's Wiki
- Colors: Font color makes use of 30 to 37. Background color makes use of 40 to 47. A 0 (or no value given) for the font color resets it to the default, which depends on the setup. For additional rendering options, 1 sets the font to bright / bold, 4 sets it to underlined, 7 sets it to negative colors.
-
Generate dummy file:
- Linux:
fallocate -l size filename
- OS:
mkfile size filename
- Test the existence of a symbol link : Note that the normal
-ewon't work for the symbol link, so we need to use -hin this case.
-
Using pandoc to write beamer!!!!! must see!!!!
beamer script demo
pandoc -t beamer slides -o output.pdf
dirs : this command is similar to pwdbut only this use ~ instead of the full path
-
some shortcuts of bash
-
Ctrl + A Go to the beginning of the line you are currently typing on
-
Ctrl + E Go to the end of the line you are currently typing on
-
Ctrl + L Clears the Screen, similar to the clear command
-
Ctrl + U Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
-
Ctrl + H Same as backspace
-
Ctrl + R Let’s you search through previously used commands
-
Ctrl + C Kill whatever you are running
-
Ctrl + D Exit the current shell
-
Ctrl + Z Puts whatever you are running into a suspended background process. fg restores it.
-
Ctrl + W Delete the word before the cursor
-
Ctrl + K Clear the line after the cursor
-
Ctrl + T Swap the last two characters before the cursor
-
Esc + T Swap the last two words before the cursor
-
Alt + F Move cursor forward one word on the current line
-
Alt + B Move cursor backward one word on the current line
-
Tab Auto-complete files and folder names
${FIRST:-$SECOND} : if the $FIRST variable is empty, then return the $SECOND
-
Some skills to deal with the input parameters of a bash function
shift NWill drop the first $N parameters in the queue, therefore we can use $1 as the variable name and a loop to deal with the inputs
$@represents all the inputs. can be echoed
$0is the name of the script
exportmakes a variable visible to child process only but not to parent process.
- About the parameter substitution in bash script : check here
- To disable a user from login with password :
sudo passwd -l username -l means lock. After this the user can only be accessed either with ssh_key or sudo su username
- The access mode of .ssh folder shall be 700 for it to be functional
- To unmount a disk:
umount path-to-mount-point or /dev/sdx
- To force unmount a disk:
fuser -km path-to-mount-point This will help to kill the processes currently using the mounted disk
- Parentheses:
[] and [[]]: Typical tests are whether a file exists or one number is equal to another.
(()): Performs arithmetic. Exit code zero(true) if the result is non-zero, but exits non-zero(false) if result is zero.
(): Run command in a sub shell