[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] FUNCNAME[0]: unbound variable
From: |
Eduardo A . Bustamante López |
Subject: |
Re: [Help-bash] FUNCNAME[0]: unbound variable |
Date: |
Sun, 8 Mar 2015 11:54:50 -0600 |
User-agent: |
Mutt/1.5.20 (2009-12-10) |
> While write scripts I usually set my PS4 to: '+(${BASH_SOURCE##*/}:${LINENO}):
> ${FUNCNAME[0]}($?):' to have nice printout of what my script does when it
> executes. I've read in a blog post that it's good habit to: "set -o
> nounset" and "set -o errexit" , so I started adding them to my scripts, but
> the combination of my PS4 + 'set -o nounset' aborts the script immediately
> with the error: "FUNCNAME[0]: unbound variable".
I'd question the validity of such blog post. set -e is actually a very weird
feature, and robust scripts shouldn't rely on it. Please read this:
http://mywiki.wooledge.org/BashFAQ/105
Now, if you still want to go that path, here's a work-around:
address@hidden ~ % PS4='+${FUNCNAME-unset}| ' bash -euxc 'f(){ :; }; echo a;
echo b; f'
+unset| echo a
a
+unset| echo b
b
+unset| f
+f| :
The ${var-default} expansion avoids the issues with the combination of set -e
and set -u.