A collection of useful code for Bash.
Declare a variable and set its value, but only if the variable is empty or undefined. It uses indirect parameter expansion. The ! means set the variable name, not just the value.
# Required arguments:
# 1. Variable name
# 2. Variable value
#
# Usage:
#
# setvar foo bar
#
# In the above example, if a variable named foo
# exists and is not empty it will be set to bar.
# Otherwise its value will not be altered.
setvar() {
eval "${1}=\"${!1:-${2}}\"";
}
When a script gets invoked via an alias $PWD returns the directory where the alias resides, not the script being called. To find the full canonical path to the current script use:
BASEPATH=$(dirname $0)
echo "$BASEPATH"
if [[ $DIR =~ .*/$ ]]; then
DIR="${DIR:0:-1}"
fi
filename="${filepath##*/}"
name="${filename%%.*}"
This looks for the existence of its shell command:
if [[ $(type curl) ]]; then
echo " Curl installed!"
fi
For other app types use command with the -v flag to prevent the application from being launched:
if command -v inkscape &>/dev/null; then
echo "Inkscape installed!"
fi
# -t means remove trailing newlines
readarray -t MYARRAY < /path/to/filename
i=0
declare -A MY_ARRAY
for file in /path/to/*.txt; do
if [[ -f "$file" ]]; then
MY_ARRAY[${i}]="$file"
((i++))
fi
done
MYARRAY=('one' 'two' 'three' 'four' 'five' 'six')
for (( i=${#MYARRAY[@]}-1 ; i>=0 ; i-- )) ; do
echo "${MYARRAY[i]}"
done
MYARRAY=(1, 2, 3, 4, 5, 6)
count="${#MYARRAY[@]}"
echo "$count"
if [[ ${#MY_ARRAY[@]} -eq 0 ]]; then
echo "Array empty"
fi
for filepath in /path/to/*.png; do
echo "$filepath"
done
for ((i=1; i<=10; i++))
{
if (( $i%2 == 0 )); then
echo "Even"
else
echo "Odd"
fi
}
MYVAR=${MYVAR//$'\n'/}
Using sed
str="Hello, my name is Foo"
str=$(echo $str | sed "s/[ ]//g")
echo $str # Hello,mynameisFoo
str="John, at the Bar is a friend-of-mine"
str=${str,,} # lowercase
str=${str//,/} # remove commas
str=${str//-/} # remove dashes
str=${str// /} # remove spaces
echo $str # johnatthebarisafriendofmine
str="Nancy Davis # Will bring jello to the potluck"
str=$(echo $str | sed "s/#.*//")
echo $str # Nancy Davis
MYVAR=${MYVAR,,} # Lowercase all characters
MYVAR=${MYVAR^^} # Uppercase all characters
MYSTRING="foobarbaz"
$MYSTRING="${MYSTRING:0:3}" # returns "foo"
$MYSTRING="${MYSTRING:6:3}" # returns "baz"
if [[ $MYVAR =~ ^?[0-9]+$ ]]; then
echo "Only integers allowed!"
fi
MYSTRING=$(echo "$MYSTRING" | sed 's/.*[[:space:]]\(#[a-zA-Z0-9]\+\)[[:space:]].*/\1/')
MYVAR=${MYVAR//&/\\&} # Excape ampersands
SEARCHFOR="bar"
if echo "$MYSTRING" | egrep -q "(^|\s)${SEARCHFOR}($|\s)"; then
echo "$SEARCHFOR exists!"
fi
MYVAR="this is some text /path/to/foo.jpg"
MYVAR=$(echo $MYVAR | sed 's/^[^/]*\///g')
MYVAR="foo:bar"
MYVAR=$(echo $MYVAR | sed "s/[:].*//")
echo $(( 5+2 ))
n=5
n=$(( n-1 ))
RND=$(shuf -i 1-20 -n 1)
echo "$RND"
By name:
pkill foo
To look up a process by name
pgrep foo
To look up all process IDs associated with a package and kill them:
PID=$(pgrep foo)
if ! [ -z "$PID" ]; then
for id in $PID; do
kill "$id"
done
fi
echo() (
fmt=%s
end=\\n
IFS=" "
while [ $# -gt 1 ] ; do
case "$1" in
[!-]*|-*[!ne]*) break ;;
*ne*|*en*) fmt=%b
end='' ;;
*n*) end='' ;;
*e*) fmt=%b ;;
esac
shift
done
printf "$fmt$end" "$*"
)
IFS=$'\n'
ls >/dev/null 2>&1
echo $? # Returns 0
fofofofo >/dev/null 2>&1
echo $? # Returns 127 (command not found)