I was trying to test for noclobber option using: if [ -o noclobber ] but it turned nothing.

I set the noclobber option on using: set +o noclobber.

prompt> cat checkoption.sh
if [ -o noclobber ]
          echo "Option is on."
prompt> set +o noclobber
prompt> set -o | grep noglobber
noclobber       on
prompt> ./checkoption.sh

Any idea why I am not getting a message here?


Shell options are not inherited between shell sessions, and you are dealing with two shell sessions here:

  1. The interactive session where you set your shell option, and
  2. The shell script's session in which you test for the option.

The shell script will never detect that the option was set in the calling interactive shell session.


  1. Turn your code into a shell function (in e.g. $HOME/.bashrc):

    checknoclobber () { [ -o noclobber ] && echo 'Noclobber is on'; }

    Or, for the generic case,

    checkoption () {
        if [ -o "$1" ]; then
            printf '%s is set\n' "$1" >&2
            return 0
            printf '%s is not set (or invalid option name)\n' "$1" >&2
            return 1
  2. Set the option in the script.

  3. Source the script file in the interactive shell with source or with the . command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.