Set Flags

set_flags.bsh

Functions to make settings/clearing bash flags easy

Sometimes a flag needs to be set locally for a piece of code, and should be reset if and only if it wasn’t set before. These functions do remember the original state so that it can be reset with a simple call

Example

set_flag u
# <code here>
reset_flag u

set_flag e
unset_flag E
set_flags xv
# <code here>
reset_flags
_save_flag
Arguments:

$1 - Single letter flag to save

Output:

Stores original state in VSI_COMMON_SET_FLAG_<flag>.

Saves the previous state of a flag

Note

The original state is smart enough to not be overridden by multiple calls to set_flag/unset_flag. The original state means the state before ALL set_flag and unset_flags were made. And is remembered until reset_flag is called.

set_flag
Arguments:

$1 - Single letter flag to set

Output:

Stores original state in VSI_COMMON_SET_FLAG_<flag>

Turns on a bash option, remembers the previous state

Note

The original state is smart enough to not be overridden by multiple calls to set_flag/unset_flag. The original state is the state before ALL set_flag and unset_flag` calls were made, and is remembered until reset_flag is called.

Bugs

Doesn’t support o flag. The only options that are not duplicated are: vi, emacs, history, pipefail, and posix. Only pipefail and posix would be of interest, and are stored in SHELLOPTS.

unset_flag

Same as set_flag, but unsets the flag

reset_flag

Resets the flag to the previous state

Sets (or unsets) the flag to the state before the very first call to set_flag/unset_flag. Also clears the variable VSI_COMMON_SET_FLAG_<flag>

set_flags
Arguments:

$1 - Multiple letters allowed

Same as set_flag, but sets multiple flags at once

unset_flags
Arguments:

$1 - Multiple letters allowed

Same as unset_flag, but unsets multiple flags at once

reset_flags
Arguments:

$1 - Multiple letters allowed

Same as reset_flag, but resets multiple flags at once

Note

If no arguments are given, all flags are reset

_save_optflag
Output:

Stores original state in VSI_COMMON_SET_OPTFLAG_<flag>.

shopt version of _save_flag

set_optflag

shopt version of set_flag

unset_optflag

shopt version of unset_flag

reset_optflag

shopt version of reset_flag

set_optflags
Arguments:

$1[...] - Multiple arguments allowed

Same as set_optflag, but set multiple opt flags at once

unset_optflags
Arguments:

$1[...] - Multiple arguments allowed

Same as unset_optflag, but unsets multiple opt flags at once

reset_optflags
Arguments:

$1[...] - Multiple arguments allowed

Same as reset_optflag, but unsets multiple opt flags at once

Note

If no arguments are given, all opt flags are reset

_save_oflag
Output:

Stores original state in VSI_COMMON_SET_OFLAG_<flag>.

set -o version of _save_flag

Bugs

Since so many of the set flags overlap with “set -o” flags, these two features should not be mixed together and used simultaneously. The only exceptions are the “set -o” flags with no equivalent single letter representation, mainly pipefail and posix

set_oflag

set -o version of set_flag

unset_oflag

set -o version of unset_flag

reset_oflag

set -o version of reset_flag

Bugs

Resetting certain flags does not return everything to status quo. For example, setting vi mode, and then resetting does not restore emacs mode. This is the only known bug, and is not important from a scripting point of view.

set_oflags
Arguments:

$1[...] - Multiple arguments allowed

Same as set_oflag, but sets multiple opt flags at once

unset_oflags

Same as unset_oflag, but unsets multiple opt flags at once

reset_oflags

Same as reset_oflag, but reset multiple flags at once

Note

If no arguments are given, all opt flags are reset