What does passing the -xe parameters to /bin/bash do

If you read the man page for bash you’ll find the following at the top of the OPTIONS section: All of the single-character shell options documented in the description of the set builtin command can be used as options when the shell is invoked. In addition, bash interprets the following options when it is invoked… … Read more

Clean way to write complex multi-line string to a variable

This will put your text into your variable without needing to escape the quotes. It will also handle unbalanced quotes (apostrophes, i.e. ‘). Putting quotes around the sentinel (EOF) prevents the text from undergoing parameter expansion. The -d” causes it to read multiple lines (ignore newlines). read is a Bash built-in so it doesn’t require … Read more

Check if array is empty in Bash

Supposing your array is $errors, just check to see if the count of elements is zero. if [ ${#errors[@]} -eq 0 ]; then echo “No errors, hooray” else echo “Oops, something went wrong…” fi

How to create a UUID in bash?

See the uuidgen program which is part of the e2fsprogs package. According to this, libuuid is now part of util-linux and the inclusion in e2fsprogs is being phased out. However, on new Ubuntu systems, uuidgen is now in the uuid-runtime package. To create a uuid and save it in a variable: uuid=$(uuidgen) On my Ubuntu … Read more

How do I get the current Unix time in milliseconds in Bash?

This: date +%s will return the number of seconds since the epoch. This: date +%s%N returns the seconds and current nanoseconds. So: date +%s%N | cut -b1-13 will give you the number of milliseconds since the epoch – current seconds plus the left three of the nanoseconds. and from MikeyB – echo $(($(date +%s%N)/1000000)) (dividing … Read more