Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POSIX sh check (test) value of builtin set option

Tags:

posix

sh

In POSIX sh you may set options with set:

#!/bin/sh

set -u;

echo "$notset";

that gives expected:

parameter not set or null

but how to check if option -e is set or not?

I want at some point of my script to turn it off but set it back to on only if it was previously on.

like image 814
Jimmix Avatar asked Nov 17 '25 10:11

Jimmix


1 Answers

The shell options are held in $- as a string of single characters. You test for -e with

case $- in
(*e*)    printf 'set -e is in effect\n';;
(*)      printf 'set -e is not in effect\n';;
esac
like image 175
Jens Avatar answered Nov 19 '25 08:11

Jens