What's a platform safe way to use Bash Shell Builtin command ?

The best way to use the shell is to use pipes to combine simple single-purpose programs (filters).

dash and ksh are safe, because they don't allow defining a function named unset, as you've discovered, and any alias form can be bypassed by invoking as \unset.

bashwhen in POSIX compatibility mode, allows you to define a function named unset, but ignores it when you invoke unset, and always executes the builtin, as you've later discovered.

with no control over the execution environment, you cannot write shell scripts that are fully immune to tamperingunless you know that your code will be executed by dashksh, or bash (with the workaround in place).
  • If you're willing to assume that unset has not been tampered with, the most robust approach is to:
    • Use \unset -f to ensure that unalias and command are unmodified (not shadowed by a shell function: \unset -f unalias command)
      • Functions, unlike aliases, must be undefined explicitly, by name, but not all shells provide a mechanism to enumerate all defined functions, unfortunately (typeset -f works in bashksh, and zsh, but dash appears to have no mechanism at all), so that undefining all functions is not always possible.
    • Use \unalias -a to remove all aliases.

1 comment:

  1. Excellent content. I have bookmarked your blogspot. Greetings JennyP

    ReplyDelete