Bash
Bash⚑
Usage⚑
Run a command N times⚑
seq [N] | xargs -Iz [command]
Output redirection⚑
- To redirect both stderr and stdout to /dev/null use:
command > /dev/null 2>&1
For loop⚑
for arg in [list]; do [command] $arg; done
Until loop⚑
Run a command until the ouptut contains some text⚑
until somecommand | grep -q "Up to date";
do
sleep 1;
done
Environment variables⚑
Remove an environment variable⚑
To remove an exported environment variable use:
unset VARIABLE
Remove prefix/suffix⚑
You can remove a fixed prefix and/or suffix from an environment variable with ${var#{prefix}}
and ${var%{suffix}}
as follows
$ string="hello-world"
$
$ foo=${string#hell}
$ foo=${foo%ld}
$
$ echo "${foo}"
o-wor
Arithmetic operations⚑
Use $(( ))
.
Get min or max from two variables⚑
Use the ternary operator. For example, for MAX(10, $VAR)
do:
$((VAR > 10 ? VAR : 10))
Locks⚑
flcok⚑
To ensure only one instance of the script runs some code, you can use flock
as follows:
(
# Wait for lock on /var/lock/.myscript.exclusivelock (fd 200) for 10 seconds
flock -x -w 10 200 || exit 1
# Do stuff
) 200>/var/lock/.myscript.exclusivelock
Tips⚑
Beep alert⚑
You can generate a beep alert (if enabled in the system) and a window highlight with:
echo -e "\a"
Configuration⚑
Prompt⚑
Add timestamp⚑
Add \D{%T}
to PS1
to display the current time (HH:MM:SS). For example for debian edit /etc/bash.bashrc and add:
PS1='\D{%T} ${debian_chroot:+($debian_chroot)}\u@\h:\w\$'