Linux/Shell
Linux/Shell
Knowledge, commands, configurations, guides and everything helpful when working with shell.
Table of Contents
- Table of Contents
- Interesting threads
- Bash
- Zsh + Oh-my-zsh + Powerlevel10k
- All Terminals
- Nano
- Top
- Man pages
Interesting threads
- https://stackoverflow.com/a/42082956/10699128
- “Difference between single and double quotes in Bash”
- Comprehensive explanation of variable evaluation
"",'',$,\
- https://unix.stackexchange.com/a/444949/379903
- “How can we run a command stored in a variable?”
- Comprehensive explanation of functions
- definition, evaluation, arguments
Bash
Make .sh script fail on error (and not continue like nothing happened)!
set -euo pipefail-e- exit immediately on error
-u- fail on unset variables
-o pipefail- fail on failed pipes
-x- before executing it command print it with arguments to stderr (like debug mode)
- More info:
https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
Complex alias
In general
1
2
3
aliasname() {
command --firstParam --secondParam
}
Example
1
2
3
getaks() {
az aks list -g $1 -o $2
}
Pass all arguments to alias
- Use
$@
1
2
3
maven() {
mvn -T1C "$@"
}
effect: maven install -DskipTests
becomes: mvn -T1C install -DskipTests
Function arguments
- List:
$@- stores all the arguments in a list of string$*- stores all the arguments as a single string$#- stores the number of arguments"$@"is actually not a list of strings, but a list of arguments.
$@expands to the arguments passed from the caller to a function or a script.- Inside a function, it expands to the arguments passed to such function.
- If used in a script (outside a function), it expands to the arguments passed to such script.
Check if file exists
1
2
3
if ! [ -f ./resources/$ARCHIVE ]; then
curl --output ./resources localhost:8080/archive.zip
fi
Zsh + Oh-my-zsh + Powerlevel10k
- You can shorthand autocomplete paths inside commands, like:
cd /h/k/.s/
->cd /home/komidawi/.ssh
- History search by starting fragment
- Example:
1 2 3 4
nano /home/komidawi/.ssh/known_hosts nano /etc/hosts nano /h<UP>
will auto-complete into
1
nano /home/komidawi/.ssh/known_hosts
- Example:
- Show
nlast history entries (0shows all)history n
- Expand envs
$USER<TAB>
->komidawi
- Print parameters help
cat -<TAB>1 2 3 4 5
--help -- display help and exit --number -n -- number all output lines --number-nonblank -b -- number nonempty output lines, overrides -n --show-all -A -- equivalent to -vET --show-ends -E -- display $ at end of each line
- Use
taketomkdirandcdinto ittake folder/subfolder/finish- it also works with zip archives, git repos and url downloads
- Command history
Ctrl + R- Press again for next occurrence
- Quick
cd- You can move into folders by name of the folder without
cd
- You can move into folders by name of the folder without
- List last visited dirs
d(shortcut fordirs -v)
- Command parking
- Press
Ctrl + Qto hide current command - After invoking another command, the first one comes back
- Press
- Open command in editor
Ctrl + X,Ctrl + E- Opens in default editor (
$EDITOR) - Useful for long commands
- cd shortcuts
cd ....
-> moves 4 levels upcd -
-> jumps to previous path
- Extensive search
ls *.txt
-> text filesls **/*.txt
-> text files (also within subdirectories)ls **/(START)*.*-> files starting with word STARTls **/*(END).*-> files ending with word ENDls **/*(WORD)*.*-> files with WORDls **/*(.)-> search only filesls **/*(/)-> search only folders
- List recently open directories
d
- Copy to clipboard (WSL2)
command | clip.exe
All Terminals
- Pretty format JSON
- Use JQ, e.g.:
cat file.json | jq
- Just colorize:
jc -c
- Use JQ, e.g.:
Nano
- Search a phrase
Ctrl + W
- Search next occurrence
Ctrl + WF6
- Previous occurrence
Alt + Q
- Show help
F1
- Show navigation help
h
Top
- Show process tree
Shift + V
- Sort by CPU usage
Shift + P
- Sort by Memory usage
Shift + M
- Kill process
k
Man pages
- Search
/<phrase>,Enter
- Next occurrence
n