Last active
November 18, 2021 02:45
-
-
Save Checksum/687c89c6c1c55fc285ef65a67f8eb820 to your computer and use it in GitHub Desktop.
Bash snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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