Skip to content

Instantly share code, notes, and snippets.

@Checksum
Last active November 18, 2021 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Checksum/687c89c6c1c55fc285ef65a67f8eb820 to your computer and use it in GitHub Desktop.
Save Checksum/687c89c6c1c55fc285ef65a67f8eb820 to your computer and use it in GitHub Desktop.
Bash snippets
#!/bin/bash
# Dynamic bracket expansion
eval echo "{1..3}"
eval echo "foo-{bar,baz}-{1..3}"
# Merge JSON objects
merged="$(jq -ers '.[0] * .[1]' <(echo '{"name": "foo"}') <(echo '{"age": "baz"}') 2>/dev/null)"
# Check if string is valid JSON
if ! jq -e 'type == "object"' <<< "$string" &>/dev/null; then
echo "Invalid payload: $payload"
exit 1
fi
# Merge and sort array
IFS="$delimiter" read -r -a arr1 <<< "$1"
IFS="$delimiter" read -r -a arr2 <<< "$2"
# Merge and deduplicate
local merged=("${arr1[@]}" "${arr2[@]}")
# shellcheck disable=2207
merged=($(printf "%s\n" "${all[@]}" | sort -u))
# Get nth line of a file
tail -n+2 file | head -n1
# URL decode
# https://stackoverflow.com/a/37840948
urldecode() {
: "${*//+/ }"
echo -e "${_//%/\\x}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment