Skip to content

Instantly share code, notes, and snippets.

@ormaaj
Created October 29, 2025 12:57
Show Gist options
  • Select an option

  • Save ormaaj/2e30faf0acf59839c6abb1c09513798f to your computer and use it in GitHub Desktop.

Select an option

Save ormaaj/2e30faf0acf59839c6abb1c09513798f to your computer and use it in GitHub Desktop.
simple shopt toggler
#!/usr/bin/env bash
shopt -s lastpipe extglob expand_aliases
function unset2 {
command unset "$@"
}
function localopt {
typeset IFS OPTARG REPLY r os=:v:uso
typeset -i i dn ret=0 OPTIND=1
typeset -A o
while getopts -- "$os" REPLY; do
typeset -Gn r
case $REPLY in
[:?]) return 2 ;;
[us]) printf -v r 'o[%x]' "${REPLY/#/\'}" ;;
[vo]) r=o[\$REPLY]
esac
r=$OPTARG
unset -v OPTARG
unset -n r
done
command shift "$((OPTIND - 1))"
unset2 -v OPTARG OPTIND os
typeset -Gn r=${o[v]}
[[
${r@a} != *A* ||
( -v o[73] && -v o[75] )
]] && return 2
for REPLY do
[[ $REPLY == +([_[:lower:]]) ]] || { ret=1; continue; }
shopt "-q${o[o]+o}" -- "$REPLY"
[[ -v 'o[$((i = (2 * $? + 73)))]' ]] ||
printf -v "r[\$REPLY]" %b "${i/#/\\x}" ${| unset2 -v REPLY o i IFS; }
done
return "$(( ! ${#r[@]} || ret ))"
}
function m {
if [[ ${FUNCNAME[0]} != "${FUNCNAME[1]}" ]]; then
typeset -gx BASH_COMPAT=51;
typeset -A lopt
localopt -v lopt -s dotglob extglob lastpipe expand_aliases sourcepath || return
shopt -s -- "${!lopt[@]}"
"${FUNCNAME[0]}" "$@"
shopt -u -- "${!lopt[@]}"
return
fi
echo baa!
}
set -x
m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment