[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] ${var:?message} customization
From: |
Greg Wooledge |
Subject: |
Re: [Help-bash] ${var:?message} customization |
Date: |
Thu, 14 May 2015 08:23:26 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Wed, May 13, 2015 at 08:54:09PM -0300, Matias A. Fonzo wrote:
> Normally, "${var:?'message'}" display error if null or unset if
> 'message' is omitted. But if it is not, a particular message can be
> included, in any case, shows (e.g):
>
> myscript: line 2: var: parameter null or not set
>
> My question is, there is a way to print only the message? (removing
> myscript: line number: var:)
Are you just asking "How can I write a message if a variable is unset,
or set to an empty string?"
if [ -z "$var" ]; then
echo "var is either empty or unset"
fi
If you want to distinguish between "unset" and "set to an empty string",
you can use a more subtle test:
if [ "${var+defined}" ]; then
if [ -z "$var" ]; then
echo "var is set to an empty string"
else
echo "var is set to a non-empty string"
fi
else
echo "var is unset"
fi
(That's all using POSIX sh syntax. Recent versions of bash have a
"test -v" command, but I don't think it adds much utility.)
Re: [Help-bash] ${var:?message} customization, Matias A. Fonzo, 2015/05/14