Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • Interesting, thanks! I seems weird to me that bash would default to not protecting unset et al from being overwritten. Commented Mar 5, 2015 at 13:40
  • This needs the -f argument to unset doesn't it? Otherwise if both a variable and function named the same as a builtin are defined then unset will pick the variable to unset first. Also this fails if any of the obscuring functions is set readonly doesn't it? (Though that's detectable and can be made a fatal error.) Also this misses the [ builtin it seems. Commented Dec 3, 2015 at 18:57
  • 1
    I don't think \unset -f unset makes sense, given it will be done in the read loop. If unset is a function your \unset would normally resolve to it instead of the builtin, but you can use \unset safely so long as POSIX mode is in effect. Explicitly calling \unset -f on [, ., and : might be a good idea though, as your regex excludes them. I would also add -f to the \unset in the loop, and would \unset POSIXLY_CORRECT after the loop, instead of before. \unalias -a (after \unset -f unalias) also allows one to safely forego the escape on subsequent commands. Commented Apr 26, 2017 at 4:20
  • 1
    @AdrianGünter Try: [[ ${POSIXLY_CORRECT?non-POSIX mode shell is untrusted}x ]], this will cause a non-interactive script to exit with the error indicated if the variable is unset, or continue if it is set (empty, or any value). Commented Jul 26, 2018 at 9:31
  • 1
    "you must use \unset" ... It should be noted that quoting of any character(s) would work to avoid the alias expansion, not just the first. un"se"t would work just as well, abeit less readable. "The first word of each simple command, if unquoted, is checked to see if it has an alias." –Bash Ref Commented Jan 31, 2019 at 18:55