Basics
~
Current user home directory~john
Userjohn
home directory~-
$OLDPWD
~+
$PWD
xsel -b file.txt
Copy to bufferset -o vi
Set the editor of the command line to vi
Change file extension
mv file.{txt,csv}
Rewrite all extensions
for f in * ; do mv "$f" "${f/.csv/.txt}" ; done
<file while read line ; do echo $line ; done
cat <<<wut
cat <(echo wut)
diff foo <(sort foo)
Get the extension
echo "${foo##*.}"
echo "$foo" | rev | cut -f 1 -d '.' | rev
Omit the extension
echo "${foo%.*}"
Write ones
tr '\0' '\377' < /dev/zero
tr '\0' '\377' < /dev/zero | dd of=ones
Error handling
#!/usr/bin/env bash
set -euo pipefail
e
: Exit immediately on non-zero returnu
: Treat empty variables as errorso
:pipefail
: Piping stops when something errors outb
: Notify of job termination immediately.v
: Print shell input lines as they are read.x
: Print commands and their arguments as they are executed.C
: Prevent existing files from being overwritten by the shell's '>' redirection operatorn
: Read commands but does not execute them
All options
man set
Traps
man trap
trap 'read foo; echo "-$foo-"' 0 # Executed when exit code is 0
trap 'eval " $cmd"' 0 # Executed when exit code is 0
trap 'error_exit ${LINENO}' ERR # Executed on error
trap 'error_exit ${BASH_COMMAND}' ERR # Executed on error
trap 'echo "removing $TMPFILE"; rm -f $TMPFILE' INT TERM EXIT q
Restart a program if it crashes
while ! discord ; do : ; done
Grab the first line
head -n 1 longfile
cat longfile | head -n 1
cat longfile 2>&1 | head -n 1
awk 'FNR <= 1' longfile
Grab the Nth line
awk 'FNR == 74' longfile
Grab string between X and Y
echo "This is Xthe stringY I want" | cut -d "X" -f2 | cut -d "Y" -f1
echo "This is Xthe stringY I want" | awk -F'[XY]' '{print $2}'
echo "This is Xthe stringY I want" | sed 's/.*\X\([^]]*\)\Y.*/\1/g'
Menu
#!/usr/bin/env sh
items="1 item number 0
2 item number 1
3 item number 2"
echo "$items"
read -p "Selection? " answer
case "$answer" in
1 ) echo 1 ;;
2 ) echo 2 ;;
3 ) echo 3 ;;
* ) echo other ;;
esac
Arguments
#!/usr/bin/env sh
while [ $# -gt 0 ]; do
case "$1" in
-h | --help | h | [hH]elp | [hH]* )
echo "help"
exit ;; ## Stop the execution
-a | --aa )
echo "Option a"
shift ;; ## Next argument
-b | --bb )
echo "Option b" ; shift ;; ## One line
-d | --dir )
shift
DIR="$1" ## Save the value in a variable
echo "Directory: $DIR"
shift ;;
* ) ## Anything else
echo "Unknown option $1"
shift ;;
esac
done
CSV file
IFS
denotes the character used to separate fields in the csv file.
#!/usr/bin/env sh
while IFS=';' read -r time date var3 location ; do
echo "The time is $time"
echo "The date is $date"
echo "The third filed in the csv file is $var3"
echo "Finally $location"
done < file.csv
Function to add text to a varible
Instead of doing echo "Text" >> output.txt
you can create a variable to hold the text and write it at the end of the script, instead of multiple times during the execution of the script.
#!/usr/bin/env bash
add() {
document="${document}
${1}"
}
add "text to add to the variable"
echo "$document" > output.txt
While read from the output of another command
cat file.txt | while IFS='' read -r line ; do
echo "Line: $line"
done
cat file.txt | while IFS=';' read -r name id position ; do
echo "$name is at $position with ID $id"
done
while IFS='' read -r line ; do
echo "Line: $line"
done < "$( cat file.txt )"
While read from file
while IFS="" read line ; do
echo "line: ${line}"
done < file.txt
Change directory in a script
In bash when you execute a script it executes in your current path. To make it run in the path the script is contained add the following at the beginning of the script.
cd "$( dirname "$0" )" || exit 1
Logical operators
true && echo here
---
here
true || echo here
---
false && echo here
---
false || echo here
---
here
true && false || true
echo 1 && (echo 2 ; false) || echo 3
---
1
2
3
true || false && true
echo 1 || (echo 2 ; false) && echo 3
---
1
3
false && true || true
( echo 1 ; false ) && echo 2 || echo 3
---
1
3
false || true && true
( echo 1 ; false ) || echo 2 && echo 3
---
1
2
3
( false || true ) && true
( ( echo 1 ; false ) || echo 2 ) && echo 3
---
1
2
3
...
Other
|&
Pipestdout
andstderr