Skip to content

gmichel-csaa/cheatsheet-shell-A4

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

1 Shell CheatSheet

linkedin
github
slack


PRs Welcome

File me Issues or star this repo.

1.1 Basic

NameComment
Redirect stdout/stderrls /tmp >/dev/null 2>&1
Deal with filenamebasename $f, dirname $f
Use timeout: avoid command hangtimeout 10 sh -c ‘ls -lt’
Restart shell without killing terminalexec -l $SHELL
Run sub-shellecho $BASH_SUBSHELL; ( echo "Running in subshell: $BASH_SUBSHELL" )
Set pipefailset -ueoxv pipefail, set +ueoxv pipefail
Shell match regexpecho $str, then grep "$regexp"
Shell match regexpexpr match "$date" "^[0-9]\{8\}" >/dev/null && echo yes
Run static code checkLink: shellcheck

1.2 Shell script

NameComment
Trap exit signalcode/trap-exit.sh
Shell retrycode/shell-retry.sh
Check if a string contains a substringcode/string-contains.sh
Check if a string in a listcode/string-in-list.sh, Link: stackoverflow
Log with timestampcode/log-with-timestamp.sh
Quit if current user is not rootcode/assert-user-root.sh
Set -x on flycode/restore-debug-output.sh
Shell run curl checkcode/curl-test.sh

1.3 Environment variables

NameComment
List all environment variablesexport
Define a new env variableexport NAME1=”value1”
Define a variable with default value of othersexport MYVAR=”${MYVAR:-$OTHERVAR}”

1.4 iterm

NameComment
Delete a wordCtrl+w

1.5 zsh

NameComment
Disable all zsh’s autocorrectIn ~/.zshrc, unsetopt correct_all
Disable a given autocorrectIn ~/.zshrc, alias ssh=’nocorrect ssh’. zsh_disable

1.6 GNU tools

1.6.1 Echo string

NameComment
Echo red textecho -e “hello,\e[0;31m there \e[0;31m”
Echo multiple linesecho -e “hello,\ndenny”
Echo bold textecho -e hello, “\033[1mThis is bold text.\033[0m”
Echo underlined textecho -e hello, “\033[4mThis is underlined text.\033[0m”

1.7 Shell Basic

1.7.1 cd

NameComment
Go to given foldercd /var/log/
Go to folder in subshell(cd /var/log/ && ls) After this, PWD won’t be changed
Go to homecd
Go to parent foldercd ..
Go to previous foldercd -

1.7.2 Numeric

NameComment
*expr 5 \* 4
+let z=x+y, z=$x+$y
==int1 -eq int2, [ $? -eq 0 ] && echo "good"
>=int1 -ge =int2
>int1 -gt =int2
<=int1 -le =int2
<int1 -lt =int2
!=int1 -ne =int2

1.7.3 xargs

# Run grep for files filtered by find
find /var/log -name "*.log" | xargs grep -i error

# Loop with pipes
cat /etc/passwd | awk -F':' '{print $1}' | xargs -I{} sudo -l -U {} | grep -v "not allowed to"

1.8 Scripts

  • Compare command output
[ 0 -eq $(find ./data -name "*.txt" -type f -print | wc -l) ]
  • get ip from eth0
/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'

1.9 More Resources

License: Code is licensed under MIT License.

linkedin github slack

Releases

No releases published

Packages

No packages published

Languages

  • Shell 100.0%