Skip to content

Instantly share code, notes, and snippets.

@konsolebox
Last active May 26, 2026 01:50
Show Gist options
  • Select an option

  • Save konsolebox/2018ae0013bc1aa378c272440bc98701 to your computer and use it in GitHub Desktop.

Select an option

Save konsolebox/2018ae0013bc1aa378c272440bc98701 to your computer and use it in GitHub Desktop.
#!/bin/bash
test_args=(
# 1-5: Standard system/portage strings
"sys-apps/gentoo-functions"
"--verbose"
"USE=unicode"
"CHOST=x86_64-pc-linux-gnu"
"FEATURES=sandbox"
# 6-10: File paths and patterns
"/var/db/repos/gentoo"
"*.patch"
"./configure"
"MAKEOPTS=-j16"
"LDFLAGS=-Wl,-O1"
# 11-15: Escapes, spaces, quotes, and symbols
"escape_\E_sequence"
"There's more to life than this"
"space separated arg"
'nested"quotes"here'
"back\\slash\\test"
# 16-20: Dynamic expansion characters
'$variable_pattern'
"\${escaped_dollar}"
"simple"
"!!!"
"DONE=true"
)
_quote_args_bash() {
local str= qw qw2 w
for w; do
printf -v qw %q "$w" || return
if [[ ${qw} == "$w" ]]; then
str+=\ $w
elif [[ ${qw} == \$* ]]; then
str+=\ ${qw//\\E/\\e}
elif [[ $w == *\'* ]]; then
if printf -v qw2 %q $'\e'"$w" && [[ ${qw2} == \$\'\\[Ee]* ]]; then
str+=\ \$\'${qw2:4}
else
str+=\ ${qw}
fi
else
str+=" '$w'"
fi
done
printf %s\\n "${str# }"
}
quote_args() {
if [ ! "${POSIXLY_CORRECT}" ] && [ "${BASH}" ] &&
eval '(( BASH_VERSINFO > 3 || BASH_VERSINFO == 3 && BASH_VERSINFO[1] ))'; then
_quote_args_bash "$@"
else
declare -p POSIXLY_CORRECT
POSIXLY_CORRECT=${POSIXLY_CORRECT} shquote "$@"
fi
}
quote_args "${test_args[@]}" "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment