How do I check if a variable exists in an 'if' statement?How to test if a variable is defined at all in Bash...
When do aircrafts become solarcrafts?
Junior developer struggles: how to communicate with management?
If 1. e4 c6 is considered as a sound defense for black, why is 1. c3 so rare?
Has any spacecraft ever had the ability to directly communicate with civilian air traffic control?
What is the word which sounds like "shtrass"?
Power LED from 3.3V Power Pin without Resistor
If Melisandre foresaw another character closing blue eyes, why did she follow Stannis?
Problems with numbers (result of calculations) alignment using siunitx package inside tabular environment
Is there a QGIS plugin that reclassify raster symbology based on current extent?
How long can a 35mm film be used/stored before it starts to lose its quality after expiry?
What happens if I start too many background jobs?
Is Cola "probably the best-known" Latin word in the world? If not, which might it be?
Why is Arya visibly scared in the library in S8E3?
The barbers paradox first order logic formalization
What happened to Rhaegal?
Feels like I am getting dragged into office politics
What does air vanishing on contact sound like?
Stark VS Thanos
What is the most remote airport from the center of the city it supposedly serves?
If an enemy is just below a 10-foot-high ceiling, are they in melee range of a creature on the ground?
I caught several of my students plagiarizing. Could it be my fault as a teacher?
What happened to Ghost?
Packet sniffer for MacOS Mojave and above
Was Hulk present at this event?
How do I check if a variable exists in an 'if' statement?
How to test if a variable is defined at all in Bash prior to version 4.2 with the nounset shell option?Issue an error when using empty shell variablesHow do I check whether a variable has been passed to a function in Bash?Differences between declared, defined, and setChecking if line in file existshow to pass a mixture of arguments to a script and only of them optional -argumentBash: while loop over numbered variablesbash: declare/typeset without assignment appears to failWhat is the definition of an array being set?Is this a correct way to test if a parameter is declared?How do I make an output for grep fail?Using the test command to create a directory if it doesn't existInitializing a variable with echogrep a variable within an if statementPreform operation in bash only if a variable is less than a second variableWhy doesn't bash expand this variable when I prefix a command with a “one time variable assignment”bash + compare variable with spacesbash: [: missing `]' when the test stored in variableif else statement: check if a path stored in a variable existsvariable containing output of docker exec command malaligned
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I need to check a variable's existence in an if statement. Something to the effect of:
if [ -v $somevar ]
then
echo "Variable somevar exists!"
else
echo "Variable somevar does not exist!"
And the closest question to that was this, which doesn't actually answer my question.
shell variable test
add a comment |
I need to check a variable's existence in an if statement. Something to the effect of:
if [ -v $somevar ]
then
echo "Variable somevar exists!"
else
echo "Variable somevar does not exist!"
And the closest question to that was this, which doesn't actually answer my question.
shell variable test
If you want to set$somevarto a value/string if variable does not exist:${somevar:=42}.
– Cyrus
Jun 25 '15 at 18:49
Personally, I tend to check just for emptiness ([ -n "$var" ]or[ ! -z "$var" ]). I think the existence/nonexistence checks are too subtle, and I prefer my code coarse and simple.
– PSkocik
Dec 1 '15 at 17:12
Why do you need that vs[ -n "$var" ]? Related: stackoverflow.com/questions/3601515/…
– Ciro Santilli 新疆改造中心996ICU六四事件
Jun 28 '16 at 10:36
add a comment |
I need to check a variable's existence in an if statement. Something to the effect of:
if [ -v $somevar ]
then
echo "Variable somevar exists!"
else
echo "Variable somevar does not exist!"
And the closest question to that was this, which doesn't actually answer my question.
shell variable test
I need to check a variable's existence in an if statement. Something to the effect of:
if [ -v $somevar ]
then
echo "Variable somevar exists!"
else
echo "Variable somevar does not exist!"
And the closest question to that was this, which doesn't actually answer my question.
shell variable test
shell variable test
edited Apr 13 '17 at 12:36
Community♦
1
1
asked Jun 25 '15 at 16:18
Interesting...Interesting...
1,67531218
1,67531218
If you want to set$somevarto a value/string if variable does not exist:${somevar:=42}.
– Cyrus
Jun 25 '15 at 18:49
Personally, I tend to check just for emptiness ([ -n "$var" ]or[ ! -z "$var" ]). I think the existence/nonexistence checks are too subtle, and I prefer my code coarse and simple.
– PSkocik
Dec 1 '15 at 17:12
Why do you need that vs[ -n "$var" ]? Related: stackoverflow.com/questions/3601515/…
– Ciro Santilli 新疆改造中心996ICU六四事件
Jun 28 '16 at 10:36
add a comment |
If you want to set$somevarto a value/string if variable does not exist:${somevar:=42}.
– Cyrus
Jun 25 '15 at 18:49
Personally, I tend to check just for emptiness ([ -n "$var" ]or[ ! -z "$var" ]). I think the existence/nonexistence checks are too subtle, and I prefer my code coarse and simple.
– PSkocik
Dec 1 '15 at 17:12
Why do you need that vs[ -n "$var" ]? Related: stackoverflow.com/questions/3601515/…
– Ciro Santilli 新疆改造中心996ICU六四事件
Jun 28 '16 at 10:36
If you want to set
$somevar to a value/string if variable does not exist: ${somevar:=42}.– Cyrus
Jun 25 '15 at 18:49
If you want to set
$somevar to a value/string if variable does not exist: ${somevar:=42}.– Cyrus
Jun 25 '15 at 18:49
Personally, I tend to check just for emptiness (
[ -n "$var" ] or [ ! -z "$var" ]). I think the existence/nonexistence checks are too subtle, and I prefer my code coarse and simple.– PSkocik
Dec 1 '15 at 17:12
Personally, I tend to check just for emptiness (
[ -n "$var" ] or [ ! -z "$var" ]). I think the existence/nonexistence checks are too subtle, and I prefer my code coarse and simple.– PSkocik
Dec 1 '15 at 17:12
Why do you need that vs
[ -n "$var" ]? Related: stackoverflow.com/questions/3601515/…– Ciro Santilli 新疆改造中心996ICU六四事件
Jun 28 '16 at 10:36
Why do you need that vs
[ -n "$var" ]? Related: stackoverflow.com/questions/3601515/…– Ciro Santilli 新疆改造中心996ICU六四事件
Jun 28 '16 at 10:36
add a comment |
12 Answers
12
active
oldest
votes
In modern bash (version 4.2 and above):
[[ -v name_of_var ]]
From help test:
-v VAR, True if the shell variable VAR is set
3
Also works with single brackets:[ -v name_of_var ].
– meuh
Jun 25 '15 at 16:45
6
beware that for hashes and arrays, it returns false unless the variable has an element of key/indice "0". For namerefs, it tests whether the target is defined. It doesn't work for special parameters like$1,$-,$#...
– Stéphane Chazelas
Dec 1 '15 at 17:33
3
This feature is only in the bash builtintestor[; It is not available in/usr/bin/test. Compareman testwithhelp test.
– Mark Lakata
Aug 26 '16 at 17:46
@MarkLakata Right, because external commands cannot know the internal state of the shell.
– Chris Down
Sep 1 '16 at 15:29
umm shouldn't it always be [[ -v "$name_of_var" ]] ?
– Alexander Mills
Nov 25 '16 at 4:13
|
show 2 more comments
Depends what you mean by exists.
Does a variable that has been declared but not assigned exist?
Does an array (or hash) variable that has been assigned an empty list exist?
Does a nameref variable pointing to a variable that currently isn't assigned exist?
Do you consider $-, $#, $1 variables? (POSIX doesn't).
In Bourne-like shells, the canonical way is:
if [ -n "${var+set}" ]; then
echo '$var was set'
fi
That works for scalar variables and other parameters to tell if a variable has been assigned a value (empty or not, automatically, from the environment, assigments, read, for or other).
For shells that have a typeset or declare command, that would not report as set the variables that have been declared but not assigned except in zsh.
For shells that support arrays, except for yash and zsh that would not report as set array variables unless the element of indice 0 has been set.
For bash (but not ksh93 nor zsh), for variables of type associative array, that would not report them as set unless their element of key "0" has been set.
For ksh93 and bash, for variables of type nameref, that only returns true if the variable referenced by the nameref is itself considered set.
For ksh, zsh and bash, a potentially better approach could be:
if ((${#var[@]})); then
echo '$var (or the variable it references for namerefs) or any of its elements for array/hashes has been set'
fi
For ksh93, zsh and bash 4.4 or above, there's also:
if typeset -p var 2> /dev/null | grep -q '^'; then
echo '$var exists'
fi
Which will report variables that have been set or declared.
declare -p/typeset -pworks inbashnow too.
– cas
May 2 '16 at 20:12
1
@cas, no. Not for declared but not set variables. Trybash -c 'typeset -i a; typeset -p a'and compare withksh93orzsh.
– Stéphane Chazelas
May 2 '16 at 21:34
+1 Thanks for point me here. Also see my question unix.stackexchange.com/q/280893/674
– Tim
May 3 '16 at 20:15
@cas, that changed with bash-4.4 (released in September 2016), I've edited that in.
– Stéphane Chazelas
Nov 2 '17 at 12:55
add a comment |
As mentioned in the answer on SO, here is a way to check:
if [ -z ${somevar+x} ]; then echo "somevar is unset"; else echo "somevar is set to '$somevar'"; fi
where ${somevar+x} is a parameter expansion which evaluates to the null if var is unset and substitutes the string "x" otherwise.
Using -n, as suggested by the other answer, will only check if the variable contains empty string. It will not check its existence.
2
You need to quote$somevarto handleIFS=x. Either that or quotex.
– mikeserv
Jun 26 '15 at 0:02
1
@mikeserv Thanks, I like learning about edge cases :) Do you meanif [ -z "${somevar+x}" ]? Would the quoting still be required inside[[and]]?
– Tom Hale
Sep 3 '18 at 14:19
@TomHale - yes, in rare cases. the[ testroutines accept command line parameters, and so the usual expansions and interpretations as ordered in the usual way should be relied upon to render at invocation of the test applied that which you should cause to be read thereby by any program command line. test{!+"!"}
– mikeserv
Oct 10 '18 at 6:02
@mikeserv I guess your yes is to my 2nd question... Is the first a yes, too?
– Tom Hale
Oct 10 '18 at 14:14
1
+1; this appears to be the simplest way to do this whenset -uis in effect and the Bash version is pre-4.2.
– Kyle Strand
Jan 8 at 22:56
|
show 2 more comments
POSIXly:
! (: "${somevar?}") 2>/dev/null && echo somevar unset
or you can let your shell show the message for you:
(: "${somevar?}")
zsh: somevar: parameter not set
@mikeserv: Yeah, of course, there's many way to do it. Firstly, I think I will duplicated it with this. But in this question, the OP want to check only, he didn't claim that he want to exit or report if variable unset, so I came with a check in subshell.
– cuonglm
Jun 26 '15 at 1:46
2
Well, i know, but it's precisely because you have to do it in a subshell like that which indicates it might not be the best way to test here - that's a halt action on failure, it doesn't allow for any simple means to handle a failure. Portably even atrapcan only work on EXIT. That's all I'm saying - it just doesn't apply as a pass/fail very well. And this isn't me talking either - i've done exactly this before and it took a little comment chat just like this to convince me. So, I just thought I'd pay it forward.
– mikeserv
Jun 26 '15 at 1:51
1
@mikeserv: Well, well, it's a good point. I only wonder, does adding that can make the OP confuse with the syntax :)
– cuonglm
Jun 26 '15 at 2:24
add a comment |
printf ${var+'$var exists!n'}
...will print nothing at all when it doesn't. Or...
printf $"var does%${var+.}s exist%cn" not !
...will tell you either way.
you can use the return value of a test to dynamically expand to the appropriate format string for your condition:
[ "${var+1}" ]
printf $"var does%.$?0s exist%cn" not !
You can also make printf fail based on a substitution...
printf $"var does%${var+.}s exist%cn%.${var+b}d"
not ! \c >&"$((2${var+-1}))" 2>/dev/null
...which prints $var does not exist! to stderr and returns other than 0 when $var is unset, but prints $var does exist! to stdout and returns 0 when $var is set.
add a comment |
This simple line works (and works on most POSIX shells):
${var+"false"} && echo "var is unset"
Or, written in a longer form:
unset var
if ${var+"false"}
then
echo "var is unset"
fi
The expansion is:
- If the var has a value (even null), false is replaced
- If the var has "no value", then "no value" (null) is replaced.
The ${var+"false"} expansion expands to either "null" of "false".
Then, "nothing" or the "false" is executed, and the exit code set.
There is no need to call the command test ([ or [[) as the exit value is set by the (execution of) the expansion itself.
Yes, except in some old versions of zsh in sh emulation when$IFScontains f, a, l, s or e. Like for other answers, there's the case of arrays, hashes, or other types of variables which one may want to mention.
– Stéphane Chazelas
Dec 1 '15 at 21:00
@StéphaneChazelas I wrotemost POSIX shells.mostmeansIn the greatest number of instances, not all. ... ... So, yes, in an obscure conditionwhen $IFS contains f, a, l, s or eand for some obscure shellsome old versions of zshthis fails: What a shock!. I should assume that such bug has been solved long ago. ... ... Are you proposing that we must write code for long ago broken shells?.
– user79743
May 30 '16 at 1:43
@StéphaneChazelas Also: The question title is very specific: Bash.
– user79743
May 30 '16 at 1:46
binary zebra???
– mikeserv
Oct 16 '18 at 19:12
add a comment |
The pure shell way:
[ "${var+1}" ] || echo "The variable has not been set"
Test script:
#!/bin/sh
echo "Test 1, var has not yet been created"
[ "${var+1}" ] || echo "The variable has not been set"
echo "Test 2, var=1"
var=1
[ "${var+1}" ] || echo "The variable has not been set"
echo "Test 3, var="
var=
[ "${var+1}" ] || echo "The variable has not been set"
echo "Test 4, unset var"
unset var
[ "${var+1}" ] || echo "The variable has not been set"
echo "Done"
Results:
Test 1, var has not yet been created
The variable has not been set
Test 2, var=1
Test 3, var=
Test 4, unset var
The variable has not been set
Done
1
Fails when variable set to null string.
– Tom Hale
Sep 3 '18 at 14:22
add a comment |
With bash 4.4.19 the following worked for me. Here is a complete example
$export MAGENTO_DB_HOST="anyvalue"
#!/bin/bash
if [ -z "$MAGENTO_DB_HOST" ]; then
echo "Magento variable not set"
else
echo $MAGENTO_DB_HOST
fi
add a comment |
if set|grep '^somevar=' >/dev/null;then
echo "somevar exists"
else
echo "does not exist"
fi
add a comment |
You can't use if command to check the existence of declared variables in bash however -v option exists in newer bash, but it's not portable and you can't use it in older bash versions. Because when you are using a variable if it doesn't exists it will born at the same time.
E.g. Imagine that I didn't use or assign a value to the MYTEST variable, but when you are using echo command it shows you nothing! Or if you are using if [ -z $MYTEST ] it returned zero value!
It didn't return another exit status, which tells you that this variable doesn't exist!
Now you have two solutions (Without -v option):
- Using
declarecommand. - Using
setcommand.
For example:
MYTEST=2
set | grep MYTEST
declare | grep MYTEST
But unfortunately these commands shows you loaded functions in memory too! You can use declare -p | grep -q MYTEST ; echo $? command for cleaner result.
add a comment |
Function to check if variable is declared/unset
including empty $array=()
In addition to @Gilles's answer
case " ${!foobar*} " in
*" foobar "*) echo "foobar is declared";;
*) echo "foobar is not declared";;
esac
-- which I did not find a way for to encapsulate it within a function -- I'd like to add a simple version, which is partly based on Richard Hansen's answer, but does address also the pitfall that occurs with an empty array=():
# The first parameter needs to be the name of the variable to be checked.
# (See example below)
var_is_declared() {
{ [[ -n ${!1+anything} ]] || declare -p $1 &>/dev/null;}
}
var_is_unset() {
{ [[ -z ${!1+anything} ]] && ! declare -p $1 &>/dev/null;}
}
- By first testing if the variable is (un)set, the call to declare can be avoided, if not necessary.
- If however
$1contains the name of an empty$array=(), the call to declare would make sure we get the right result - There's never much data passed to /dev/null as declare is only called if either the variable is unset or an empty array.
With the following code the functions can be tested:
( # start a subshell to encapsulate functions/vars for easy copy-paste into the terminal
# do not use this extra parenthesis () in a script!
var_is_declared() {
{ [[ -n ${!1+anything} ]] || declare -p $1 &>/dev/null;}
}
var_is_unset() {
{ [[ -z ${!1+anything} ]] && ! declare -p $1 &>/dev/null;}
}
:; echo -n 'a; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a=; echo -n 'a=; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a="sd"; echo -n 'a="sd"; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a=(); echo -n 'a=(); '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a=(""); echo -n 'a=(""); '; var_is_declared a && echo "# is declared" || echo "# is not declared"
unset a; echo -n 'unset a; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
echo ;
:; echo -n 'a; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a=; echo -n 'a=; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a="foo"; echo -n 'a="foo"; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a=(); echo -n 'a=(); '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a=(""); echo -n 'a=(""); '; var_is_unset a && echo "# is unset" || echo "# is not unset"
unset a; echo -n 'unset a; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
)
The script should return
a; # is not declared
a=; # is declared
a="foo"; # is declared
a=(); # is declared
a=(""); # is declared
unset a; # is not declared
a; # is unset
a=; # is not unset
a="foo"; # is not unset
a=(); # is not unset
a=(""); # is not unset
unset a; # is unset
add a comment |
bash function that works for both scalar and array types:
definition
has_declare() { # check if variable is set at all
local "$@" # inject 'name' argument in local scope
&>/dev/null declare -p "$name" # return 0 when var is present
}
invocation
if has_declare name="vars_name" ; then
echo "variable present: vars_name=$vars_name"
fi
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f212183%2fhow-do-i-check-if-a-variable-exists-in-an-if-statement%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
12 Answers
12
active
oldest
votes
12 Answers
12
active
oldest
votes
active
oldest
votes
active
oldest
votes
In modern bash (version 4.2 and above):
[[ -v name_of_var ]]
From help test:
-v VAR, True if the shell variable VAR is set
3
Also works with single brackets:[ -v name_of_var ].
– meuh
Jun 25 '15 at 16:45
6
beware that for hashes and arrays, it returns false unless the variable has an element of key/indice "0". For namerefs, it tests whether the target is defined. It doesn't work for special parameters like$1,$-,$#...
– Stéphane Chazelas
Dec 1 '15 at 17:33
3
This feature is only in the bash builtintestor[; It is not available in/usr/bin/test. Compareman testwithhelp test.
– Mark Lakata
Aug 26 '16 at 17:46
@MarkLakata Right, because external commands cannot know the internal state of the shell.
– Chris Down
Sep 1 '16 at 15:29
umm shouldn't it always be [[ -v "$name_of_var" ]] ?
– Alexander Mills
Nov 25 '16 at 4:13
|
show 2 more comments
In modern bash (version 4.2 and above):
[[ -v name_of_var ]]
From help test:
-v VAR, True if the shell variable VAR is set
3
Also works with single brackets:[ -v name_of_var ].
– meuh
Jun 25 '15 at 16:45
6
beware that for hashes and arrays, it returns false unless the variable has an element of key/indice "0". For namerefs, it tests whether the target is defined. It doesn't work for special parameters like$1,$-,$#...
– Stéphane Chazelas
Dec 1 '15 at 17:33
3
This feature is only in the bash builtintestor[; It is not available in/usr/bin/test. Compareman testwithhelp test.
– Mark Lakata
Aug 26 '16 at 17:46
@MarkLakata Right, because external commands cannot know the internal state of the shell.
– Chris Down
Sep 1 '16 at 15:29
umm shouldn't it always be [[ -v "$name_of_var" ]] ?
– Alexander Mills
Nov 25 '16 at 4:13
|
show 2 more comments
In modern bash (version 4.2 and above):
[[ -v name_of_var ]]
From help test:
-v VAR, True if the shell variable VAR is set
In modern bash (version 4.2 and above):
[[ -v name_of_var ]]
From help test:
-v VAR, True if the shell variable VAR is set
answered Jun 25 '15 at 16:36
Chris DownChris Down
82.2k15192206
82.2k15192206
3
Also works with single brackets:[ -v name_of_var ].
– meuh
Jun 25 '15 at 16:45
6
beware that for hashes and arrays, it returns false unless the variable has an element of key/indice "0". For namerefs, it tests whether the target is defined. It doesn't work for special parameters like$1,$-,$#...
– Stéphane Chazelas
Dec 1 '15 at 17:33
3
This feature is only in the bash builtintestor[; It is not available in/usr/bin/test. Compareman testwithhelp test.
– Mark Lakata
Aug 26 '16 at 17:46
@MarkLakata Right, because external commands cannot know the internal state of the shell.
– Chris Down
Sep 1 '16 at 15:29
umm shouldn't it always be [[ -v "$name_of_var" ]] ?
– Alexander Mills
Nov 25 '16 at 4:13
|
show 2 more comments
3
Also works with single brackets:[ -v name_of_var ].
– meuh
Jun 25 '15 at 16:45
6
beware that for hashes and arrays, it returns false unless the variable has an element of key/indice "0". For namerefs, it tests whether the target is defined. It doesn't work for special parameters like$1,$-,$#...
– Stéphane Chazelas
Dec 1 '15 at 17:33
3
This feature is only in the bash builtintestor[; It is not available in/usr/bin/test. Compareman testwithhelp test.
– Mark Lakata
Aug 26 '16 at 17:46
@MarkLakata Right, because external commands cannot know the internal state of the shell.
– Chris Down
Sep 1 '16 at 15:29
umm shouldn't it always be [[ -v "$name_of_var" ]] ?
– Alexander Mills
Nov 25 '16 at 4:13
3
3
Also works with single brackets:
[ -v name_of_var ].– meuh
Jun 25 '15 at 16:45
Also works with single brackets:
[ -v name_of_var ].– meuh
Jun 25 '15 at 16:45
6
6
beware that for hashes and arrays, it returns false unless the variable has an element of key/indice "0". For namerefs, it tests whether the target is defined. It doesn't work for special parameters like
$1, $-, $#...– Stéphane Chazelas
Dec 1 '15 at 17:33
beware that for hashes and arrays, it returns false unless the variable has an element of key/indice "0". For namerefs, it tests whether the target is defined. It doesn't work for special parameters like
$1, $-, $#...– Stéphane Chazelas
Dec 1 '15 at 17:33
3
3
This feature is only in the bash builtin
test or [ ; It is not available in /usr/bin/test. Compare man test with help test.– Mark Lakata
Aug 26 '16 at 17:46
This feature is only in the bash builtin
test or [ ; It is not available in /usr/bin/test. Compare man test with help test.– Mark Lakata
Aug 26 '16 at 17:46
@MarkLakata Right, because external commands cannot know the internal state of the shell.
– Chris Down
Sep 1 '16 at 15:29
@MarkLakata Right, because external commands cannot know the internal state of the shell.
– Chris Down
Sep 1 '16 at 15:29
umm shouldn't it always be [[ -v "$name_of_var" ]] ?
– Alexander Mills
Nov 25 '16 at 4:13
umm shouldn't it always be [[ -v "$name_of_var" ]] ?
– Alexander Mills
Nov 25 '16 at 4:13
|
show 2 more comments
Depends what you mean by exists.
Does a variable that has been declared but not assigned exist?
Does an array (or hash) variable that has been assigned an empty list exist?
Does a nameref variable pointing to a variable that currently isn't assigned exist?
Do you consider $-, $#, $1 variables? (POSIX doesn't).
In Bourne-like shells, the canonical way is:
if [ -n "${var+set}" ]; then
echo '$var was set'
fi
That works for scalar variables and other parameters to tell if a variable has been assigned a value (empty or not, automatically, from the environment, assigments, read, for or other).
For shells that have a typeset or declare command, that would not report as set the variables that have been declared but not assigned except in zsh.
For shells that support arrays, except for yash and zsh that would not report as set array variables unless the element of indice 0 has been set.
For bash (but not ksh93 nor zsh), for variables of type associative array, that would not report them as set unless their element of key "0" has been set.
For ksh93 and bash, for variables of type nameref, that only returns true if the variable referenced by the nameref is itself considered set.
For ksh, zsh and bash, a potentially better approach could be:
if ((${#var[@]})); then
echo '$var (or the variable it references for namerefs) or any of its elements for array/hashes has been set'
fi
For ksh93, zsh and bash 4.4 or above, there's also:
if typeset -p var 2> /dev/null | grep -q '^'; then
echo '$var exists'
fi
Which will report variables that have been set or declared.
declare -p/typeset -pworks inbashnow too.
– cas
May 2 '16 at 20:12
1
@cas, no. Not for declared but not set variables. Trybash -c 'typeset -i a; typeset -p a'and compare withksh93orzsh.
– Stéphane Chazelas
May 2 '16 at 21:34
+1 Thanks for point me here. Also see my question unix.stackexchange.com/q/280893/674
– Tim
May 3 '16 at 20:15
@cas, that changed with bash-4.4 (released in September 2016), I've edited that in.
– Stéphane Chazelas
Nov 2 '17 at 12:55
add a comment |
Depends what you mean by exists.
Does a variable that has been declared but not assigned exist?
Does an array (or hash) variable that has been assigned an empty list exist?
Does a nameref variable pointing to a variable that currently isn't assigned exist?
Do you consider $-, $#, $1 variables? (POSIX doesn't).
In Bourne-like shells, the canonical way is:
if [ -n "${var+set}" ]; then
echo '$var was set'
fi
That works for scalar variables and other parameters to tell if a variable has been assigned a value (empty or not, automatically, from the environment, assigments, read, for or other).
For shells that have a typeset or declare command, that would not report as set the variables that have been declared but not assigned except in zsh.
For shells that support arrays, except for yash and zsh that would not report as set array variables unless the element of indice 0 has been set.
For bash (but not ksh93 nor zsh), for variables of type associative array, that would not report them as set unless their element of key "0" has been set.
For ksh93 and bash, for variables of type nameref, that only returns true if the variable referenced by the nameref is itself considered set.
For ksh, zsh and bash, a potentially better approach could be:
if ((${#var[@]})); then
echo '$var (or the variable it references for namerefs) or any of its elements for array/hashes has been set'
fi
For ksh93, zsh and bash 4.4 or above, there's also:
if typeset -p var 2> /dev/null | grep -q '^'; then
echo '$var exists'
fi
Which will report variables that have been set or declared.
declare -p/typeset -pworks inbashnow too.
– cas
May 2 '16 at 20:12
1
@cas, no. Not for declared but not set variables. Trybash -c 'typeset -i a; typeset -p a'and compare withksh93orzsh.
– Stéphane Chazelas
May 2 '16 at 21:34
+1 Thanks for point me here. Also see my question unix.stackexchange.com/q/280893/674
– Tim
May 3 '16 at 20:15
@cas, that changed with bash-4.4 (released in September 2016), I've edited that in.
– Stéphane Chazelas
Nov 2 '17 at 12:55
add a comment |
Depends what you mean by exists.
Does a variable that has been declared but not assigned exist?
Does an array (or hash) variable that has been assigned an empty list exist?
Does a nameref variable pointing to a variable that currently isn't assigned exist?
Do you consider $-, $#, $1 variables? (POSIX doesn't).
In Bourne-like shells, the canonical way is:
if [ -n "${var+set}" ]; then
echo '$var was set'
fi
That works for scalar variables and other parameters to tell if a variable has been assigned a value (empty or not, automatically, from the environment, assigments, read, for or other).
For shells that have a typeset or declare command, that would not report as set the variables that have been declared but not assigned except in zsh.
For shells that support arrays, except for yash and zsh that would not report as set array variables unless the element of indice 0 has been set.
For bash (but not ksh93 nor zsh), for variables of type associative array, that would not report them as set unless their element of key "0" has been set.
For ksh93 and bash, for variables of type nameref, that only returns true if the variable referenced by the nameref is itself considered set.
For ksh, zsh and bash, a potentially better approach could be:
if ((${#var[@]})); then
echo '$var (or the variable it references for namerefs) or any of its elements for array/hashes has been set'
fi
For ksh93, zsh and bash 4.4 or above, there's also:
if typeset -p var 2> /dev/null | grep -q '^'; then
echo '$var exists'
fi
Which will report variables that have been set or declared.
Depends what you mean by exists.
Does a variable that has been declared but not assigned exist?
Does an array (or hash) variable that has been assigned an empty list exist?
Does a nameref variable pointing to a variable that currently isn't assigned exist?
Do you consider $-, $#, $1 variables? (POSIX doesn't).
In Bourne-like shells, the canonical way is:
if [ -n "${var+set}" ]; then
echo '$var was set'
fi
That works for scalar variables and other parameters to tell if a variable has been assigned a value (empty or not, automatically, from the environment, assigments, read, for or other).
For shells that have a typeset or declare command, that would not report as set the variables that have been declared but not assigned except in zsh.
For shells that support arrays, except for yash and zsh that would not report as set array variables unless the element of indice 0 has been set.
For bash (but not ksh93 nor zsh), for variables of type associative array, that would not report them as set unless their element of key "0" has been set.
For ksh93 and bash, for variables of type nameref, that only returns true if the variable referenced by the nameref is itself considered set.
For ksh, zsh and bash, a potentially better approach could be:
if ((${#var[@]})); then
echo '$var (or the variable it references for namerefs) or any of its elements for array/hashes has been set'
fi
For ksh93, zsh and bash 4.4 or above, there's also:
if typeset -p var 2> /dev/null | grep -q '^'; then
echo '$var exists'
fi
Which will report variables that have been set or declared.
edited Nov 2 '17 at 12:54
answered Dec 1 '15 at 21:32
Stéphane ChazelasStéphane Chazelas
316k57600960
316k57600960
declare -p/typeset -pworks inbashnow too.
– cas
May 2 '16 at 20:12
1
@cas, no. Not for declared but not set variables. Trybash -c 'typeset -i a; typeset -p a'and compare withksh93orzsh.
– Stéphane Chazelas
May 2 '16 at 21:34
+1 Thanks for point me here. Also see my question unix.stackexchange.com/q/280893/674
– Tim
May 3 '16 at 20:15
@cas, that changed with bash-4.4 (released in September 2016), I've edited that in.
– Stéphane Chazelas
Nov 2 '17 at 12:55
add a comment |
declare -p/typeset -pworks inbashnow too.
– cas
May 2 '16 at 20:12
1
@cas, no. Not for declared but not set variables. Trybash -c 'typeset -i a; typeset -p a'and compare withksh93orzsh.
– Stéphane Chazelas
May 2 '16 at 21:34
+1 Thanks for point me here. Also see my question unix.stackexchange.com/q/280893/674
– Tim
May 3 '16 at 20:15
@cas, that changed with bash-4.4 (released in September 2016), I've edited that in.
– Stéphane Chazelas
Nov 2 '17 at 12:55
declare -p / typeset -p works in bash now too.– cas
May 2 '16 at 20:12
declare -p / typeset -p works in bash now too.– cas
May 2 '16 at 20:12
1
1
@cas, no. Not for declared but not set variables. Try
bash -c 'typeset -i a; typeset -p a' and compare with ksh93 or zsh.– Stéphane Chazelas
May 2 '16 at 21:34
@cas, no. Not for declared but not set variables. Try
bash -c 'typeset -i a; typeset -p a' and compare with ksh93 or zsh.– Stéphane Chazelas
May 2 '16 at 21:34
+1 Thanks for point me here. Also see my question unix.stackexchange.com/q/280893/674
– Tim
May 3 '16 at 20:15
+1 Thanks for point me here. Also see my question unix.stackexchange.com/q/280893/674
– Tim
May 3 '16 at 20:15
@cas, that changed with bash-4.4 (released in September 2016), I've edited that in.
– Stéphane Chazelas
Nov 2 '17 at 12:55
@cas, that changed with bash-4.4 (released in September 2016), I've edited that in.
– Stéphane Chazelas
Nov 2 '17 at 12:55
add a comment |
As mentioned in the answer on SO, here is a way to check:
if [ -z ${somevar+x} ]; then echo "somevar is unset"; else echo "somevar is set to '$somevar'"; fi
where ${somevar+x} is a parameter expansion which evaluates to the null if var is unset and substitutes the string "x" otherwise.
Using -n, as suggested by the other answer, will only check if the variable contains empty string. It will not check its existence.
2
You need to quote$somevarto handleIFS=x. Either that or quotex.
– mikeserv
Jun 26 '15 at 0:02
1
@mikeserv Thanks, I like learning about edge cases :) Do you meanif [ -z "${somevar+x}" ]? Would the quoting still be required inside[[and]]?
– Tom Hale
Sep 3 '18 at 14:19
@TomHale - yes, in rare cases. the[ testroutines accept command line parameters, and so the usual expansions and interpretations as ordered in the usual way should be relied upon to render at invocation of the test applied that which you should cause to be read thereby by any program command line. test{!+"!"}
– mikeserv
Oct 10 '18 at 6:02
@mikeserv I guess your yes is to my 2nd question... Is the first a yes, too?
– Tom Hale
Oct 10 '18 at 14:14
1
+1; this appears to be the simplest way to do this whenset -uis in effect and the Bash version is pre-4.2.
– Kyle Strand
Jan 8 at 22:56
|
show 2 more comments
As mentioned in the answer on SO, here is a way to check:
if [ -z ${somevar+x} ]; then echo "somevar is unset"; else echo "somevar is set to '$somevar'"; fi
where ${somevar+x} is a parameter expansion which evaluates to the null if var is unset and substitutes the string "x" otherwise.
Using -n, as suggested by the other answer, will only check if the variable contains empty string. It will not check its existence.
2
You need to quote$somevarto handleIFS=x. Either that or quotex.
– mikeserv
Jun 26 '15 at 0:02
1
@mikeserv Thanks, I like learning about edge cases :) Do you meanif [ -z "${somevar+x}" ]? Would the quoting still be required inside[[and]]?
– Tom Hale
Sep 3 '18 at 14:19
@TomHale - yes, in rare cases. the[ testroutines accept command line parameters, and so the usual expansions and interpretations as ordered in the usual way should be relied upon to render at invocation of the test applied that which you should cause to be read thereby by any program command line. test{!+"!"}
– mikeserv
Oct 10 '18 at 6:02
@mikeserv I guess your yes is to my 2nd question... Is the first a yes, too?
– Tom Hale
Oct 10 '18 at 14:14
1
+1; this appears to be the simplest way to do this whenset -uis in effect and the Bash version is pre-4.2.
– Kyle Strand
Jan 8 at 22:56
|
show 2 more comments
As mentioned in the answer on SO, here is a way to check:
if [ -z ${somevar+x} ]; then echo "somevar is unset"; else echo "somevar is set to '$somevar'"; fi
where ${somevar+x} is a parameter expansion which evaluates to the null if var is unset and substitutes the string "x" otherwise.
Using -n, as suggested by the other answer, will only check if the variable contains empty string. It will not check its existence.
As mentioned in the answer on SO, here is a way to check:
if [ -z ${somevar+x} ]; then echo "somevar is unset"; else echo "somevar is set to '$somevar'"; fi
where ${somevar+x} is a parameter expansion which evaluates to the null if var is unset and substitutes the string "x" otherwise.
Using -n, as suggested by the other answer, will only check if the variable contains empty string. It will not check its existence.
edited May 23 '17 at 12:40
Community♦
1
1
answered Jun 25 '15 at 16:31
shivamsshivams
2,89611427
2,89611427
2
You need to quote$somevarto handleIFS=x. Either that or quotex.
– mikeserv
Jun 26 '15 at 0:02
1
@mikeserv Thanks, I like learning about edge cases :) Do you meanif [ -z "${somevar+x}" ]? Would the quoting still be required inside[[and]]?
– Tom Hale
Sep 3 '18 at 14:19
@TomHale - yes, in rare cases. the[ testroutines accept command line parameters, and so the usual expansions and interpretations as ordered in the usual way should be relied upon to render at invocation of the test applied that which you should cause to be read thereby by any program command line. test{!+"!"}
– mikeserv
Oct 10 '18 at 6:02
@mikeserv I guess your yes is to my 2nd question... Is the first a yes, too?
– Tom Hale
Oct 10 '18 at 14:14
1
+1; this appears to be the simplest way to do this whenset -uis in effect and the Bash version is pre-4.2.
– Kyle Strand
Jan 8 at 22:56
|
show 2 more comments
2
You need to quote$somevarto handleIFS=x. Either that or quotex.
– mikeserv
Jun 26 '15 at 0:02
1
@mikeserv Thanks, I like learning about edge cases :) Do you meanif [ -z "${somevar+x}" ]? Would the quoting still be required inside[[and]]?
– Tom Hale
Sep 3 '18 at 14:19
@TomHale - yes, in rare cases. the[ testroutines accept command line parameters, and so the usual expansions and interpretations as ordered in the usual way should be relied upon to render at invocation of the test applied that which you should cause to be read thereby by any program command line. test{!+"!"}
– mikeserv
Oct 10 '18 at 6:02
@mikeserv I guess your yes is to my 2nd question... Is the first a yes, too?
– Tom Hale
Oct 10 '18 at 14:14
1
+1; this appears to be the simplest way to do this whenset -uis in effect and the Bash version is pre-4.2.
– Kyle Strand
Jan 8 at 22:56
2
2
You need to quote
$somevar to handle IFS=x. Either that or quote x.– mikeserv
Jun 26 '15 at 0:02
You need to quote
$somevar to handle IFS=x. Either that or quote x.– mikeserv
Jun 26 '15 at 0:02
1
1
@mikeserv Thanks, I like learning about edge cases :) Do you mean
if [ -z "${somevar+x}" ]? Would the quoting still be required inside [[ and ]]?– Tom Hale
Sep 3 '18 at 14:19
@mikeserv Thanks, I like learning about edge cases :) Do you mean
if [ -z "${somevar+x}" ]? Would the quoting still be required inside [[ and ]]?– Tom Hale
Sep 3 '18 at 14:19
@TomHale - yes, in rare cases. the
[ test routines accept command line parameters, and so the usual expansions and interpretations as ordered in the usual way should be relied upon to render at invocation of the test applied that which you should cause to be read thereby by any program command line. test{!+"!"}– mikeserv
Oct 10 '18 at 6:02
@TomHale - yes, in rare cases. the
[ test routines accept command line parameters, and so the usual expansions and interpretations as ordered in the usual way should be relied upon to render at invocation of the test applied that which you should cause to be read thereby by any program command line. test{!+"!"}– mikeserv
Oct 10 '18 at 6:02
@mikeserv I guess your yes is to my 2nd question... Is the first a yes, too?
– Tom Hale
Oct 10 '18 at 14:14
@mikeserv I guess your yes is to my 2nd question... Is the first a yes, too?
– Tom Hale
Oct 10 '18 at 14:14
1
1
+1; this appears to be the simplest way to do this when
set -u is in effect and the Bash version is pre-4.2.– Kyle Strand
Jan 8 at 22:56
+1; this appears to be the simplest way to do this when
set -u is in effect and the Bash version is pre-4.2.– Kyle Strand
Jan 8 at 22:56
|
show 2 more comments
POSIXly:
! (: "${somevar?}") 2>/dev/null && echo somevar unset
or you can let your shell show the message for you:
(: "${somevar?}")
zsh: somevar: parameter not set
@mikeserv: Yeah, of course, there's many way to do it. Firstly, I think I will duplicated it with this. But in this question, the OP want to check only, he didn't claim that he want to exit or report if variable unset, so I came with a check in subshell.
– cuonglm
Jun 26 '15 at 1:46
2
Well, i know, but it's precisely because you have to do it in a subshell like that which indicates it might not be the best way to test here - that's a halt action on failure, it doesn't allow for any simple means to handle a failure. Portably even atrapcan only work on EXIT. That's all I'm saying - it just doesn't apply as a pass/fail very well. And this isn't me talking either - i've done exactly this before and it took a little comment chat just like this to convince me. So, I just thought I'd pay it forward.
– mikeserv
Jun 26 '15 at 1:51
1
@mikeserv: Well, well, it's a good point. I only wonder, does adding that can make the OP confuse with the syntax :)
– cuonglm
Jun 26 '15 at 2:24
add a comment |
POSIXly:
! (: "${somevar?}") 2>/dev/null && echo somevar unset
or you can let your shell show the message for you:
(: "${somevar?}")
zsh: somevar: parameter not set
@mikeserv: Yeah, of course, there's many way to do it. Firstly, I think I will duplicated it with this. But in this question, the OP want to check only, he didn't claim that he want to exit or report if variable unset, so I came with a check in subshell.
– cuonglm
Jun 26 '15 at 1:46
2
Well, i know, but it's precisely because you have to do it in a subshell like that which indicates it might not be the best way to test here - that's a halt action on failure, it doesn't allow for any simple means to handle a failure. Portably even atrapcan only work on EXIT. That's all I'm saying - it just doesn't apply as a pass/fail very well. And this isn't me talking either - i've done exactly this before and it took a little comment chat just like this to convince me. So, I just thought I'd pay it forward.
– mikeserv
Jun 26 '15 at 1:51
1
@mikeserv: Well, well, it's a good point. I only wonder, does adding that can make the OP confuse with the syntax :)
– cuonglm
Jun 26 '15 at 2:24
add a comment |
POSIXly:
! (: "${somevar?}") 2>/dev/null && echo somevar unset
or you can let your shell show the message for you:
(: "${somevar?}")
zsh: somevar: parameter not set
POSIXly:
! (: "${somevar?}") 2>/dev/null && echo somevar unset
or you can let your shell show the message for you:
(: "${somevar?}")
zsh: somevar: parameter not set
answered Jun 25 '15 at 16:41
cuonglmcuonglm
106k25213310
106k25213310
@mikeserv: Yeah, of course, there's many way to do it. Firstly, I think I will duplicated it with this. But in this question, the OP want to check only, he didn't claim that he want to exit or report if variable unset, so I came with a check in subshell.
– cuonglm
Jun 26 '15 at 1:46
2
Well, i know, but it's precisely because you have to do it in a subshell like that which indicates it might not be the best way to test here - that's a halt action on failure, it doesn't allow for any simple means to handle a failure. Portably even atrapcan only work on EXIT. That's all I'm saying - it just doesn't apply as a pass/fail very well. And this isn't me talking either - i've done exactly this before and it took a little comment chat just like this to convince me. So, I just thought I'd pay it forward.
– mikeserv
Jun 26 '15 at 1:51
1
@mikeserv: Well, well, it's a good point. I only wonder, does adding that can make the OP confuse with the syntax :)
– cuonglm
Jun 26 '15 at 2:24
add a comment |
@mikeserv: Yeah, of course, there's many way to do it. Firstly, I think I will duplicated it with this. But in this question, the OP want to check only, he didn't claim that he want to exit or report if variable unset, so I came with a check in subshell.
– cuonglm
Jun 26 '15 at 1:46
2
Well, i know, but it's precisely because you have to do it in a subshell like that which indicates it might not be the best way to test here - that's a halt action on failure, it doesn't allow for any simple means to handle a failure. Portably even atrapcan only work on EXIT. That's all I'm saying - it just doesn't apply as a pass/fail very well. And this isn't me talking either - i've done exactly this before and it took a little comment chat just like this to convince me. So, I just thought I'd pay it forward.
– mikeserv
Jun 26 '15 at 1:51
1
@mikeserv: Well, well, it's a good point. I only wonder, does adding that can make the OP confuse with the syntax :)
– cuonglm
Jun 26 '15 at 2:24
@mikeserv: Yeah, of course, there's many way to do it. Firstly, I think I will duplicated it with this. But in this question, the OP want to check only, he didn't claim that he want to exit or report if variable unset, so I came with a check in subshell.
– cuonglm
Jun 26 '15 at 1:46
@mikeserv: Yeah, of course, there's many way to do it. Firstly, I think I will duplicated it with this. But in this question, the OP want to check only, he didn't claim that he want to exit or report if variable unset, so I came with a check in subshell.
– cuonglm
Jun 26 '15 at 1:46
2
2
Well, i know, but it's precisely because you have to do it in a subshell like that which indicates it might not be the best way to test here - that's a halt action on failure, it doesn't allow for any simple means to handle a failure. Portably even a
trap can only work on EXIT. That's all I'm saying - it just doesn't apply as a pass/fail very well. And this isn't me talking either - i've done exactly this before and it took a little comment chat just like this to convince me. So, I just thought I'd pay it forward.– mikeserv
Jun 26 '15 at 1:51
Well, i know, but it's precisely because you have to do it in a subshell like that which indicates it might not be the best way to test here - that's a halt action on failure, it doesn't allow for any simple means to handle a failure. Portably even a
trap can only work on EXIT. That's all I'm saying - it just doesn't apply as a pass/fail very well. And this isn't me talking either - i've done exactly this before and it took a little comment chat just like this to convince me. So, I just thought I'd pay it forward.– mikeserv
Jun 26 '15 at 1:51
1
1
@mikeserv: Well, well, it's a good point. I only wonder, does adding that can make the OP confuse with the syntax :)
– cuonglm
Jun 26 '15 at 2:24
@mikeserv: Well, well, it's a good point. I only wonder, does adding that can make the OP confuse with the syntax :)
– cuonglm
Jun 26 '15 at 2:24
add a comment |
printf ${var+'$var exists!n'}
...will print nothing at all when it doesn't. Or...
printf $"var does%${var+.}s exist%cn" not !
...will tell you either way.
you can use the return value of a test to dynamically expand to the appropriate format string for your condition:
[ "${var+1}" ]
printf $"var does%.$?0s exist%cn" not !
You can also make printf fail based on a substitution...
printf $"var does%${var+.}s exist%cn%.${var+b}d"
not ! \c >&"$((2${var+-1}))" 2>/dev/null
...which prints $var does not exist! to stderr and returns other than 0 when $var is unset, but prints $var does exist! to stdout and returns 0 when $var is set.
add a comment |
printf ${var+'$var exists!n'}
...will print nothing at all when it doesn't. Or...
printf $"var does%${var+.}s exist%cn" not !
...will tell you either way.
you can use the return value of a test to dynamically expand to the appropriate format string for your condition:
[ "${var+1}" ]
printf $"var does%.$?0s exist%cn" not !
You can also make printf fail based on a substitution...
printf $"var does%${var+.}s exist%cn%.${var+b}d"
not ! \c >&"$((2${var+-1}))" 2>/dev/null
...which prints $var does not exist! to stderr and returns other than 0 when $var is unset, but prints $var does exist! to stdout and returns 0 when $var is set.
add a comment |
printf ${var+'$var exists!n'}
...will print nothing at all when it doesn't. Or...
printf $"var does%${var+.}s exist%cn" not !
...will tell you either way.
you can use the return value of a test to dynamically expand to the appropriate format string for your condition:
[ "${var+1}" ]
printf $"var does%.$?0s exist%cn" not !
You can also make printf fail based on a substitution...
printf $"var does%${var+.}s exist%cn%.${var+b}d"
not ! \c >&"$((2${var+-1}))" 2>/dev/null
...which prints $var does not exist! to stderr and returns other than 0 when $var is unset, but prints $var does exist! to stdout and returns 0 when $var is set.
printf ${var+'$var exists!n'}
...will print nothing at all when it doesn't. Or...
printf $"var does%${var+.}s exist%cn" not !
...will tell you either way.
you can use the return value of a test to dynamically expand to the appropriate format string for your condition:
[ "${var+1}" ]
printf $"var does%.$?0s exist%cn" not !
You can also make printf fail based on a substitution...
printf $"var does%${var+.}s exist%cn%.${var+b}d"
not ! \c >&"$((2${var+-1}))" 2>/dev/null
...which prints $var does not exist! to stderr and returns other than 0 when $var is unset, but prints $var does exist! to stdout and returns 0 when $var is set.
edited Dec 1 '15 at 19:14
answered Jun 25 '15 at 22:32
mikeservmikeserv
46.2k669164
46.2k669164
add a comment |
add a comment |
This simple line works (and works on most POSIX shells):
${var+"false"} && echo "var is unset"
Or, written in a longer form:
unset var
if ${var+"false"}
then
echo "var is unset"
fi
The expansion is:
- If the var has a value (even null), false is replaced
- If the var has "no value", then "no value" (null) is replaced.
The ${var+"false"} expansion expands to either "null" of "false".
Then, "nothing" or the "false" is executed, and the exit code set.
There is no need to call the command test ([ or [[) as the exit value is set by the (execution of) the expansion itself.
Yes, except in some old versions of zsh in sh emulation when$IFScontains f, a, l, s or e. Like for other answers, there's the case of arrays, hashes, or other types of variables which one may want to mention.
– Stéphane Chazelas
Dec 1 '15 at 21:00
@StéphaneChazelas I wrotemost POSIX shells.mostmeansIn the greatest number of instances, not all. ... ... So, yes, in an obscure conditionwhen $IFS contains f, a, l, s or eand for some obscure shellsome old versions of zshthis fails: What a shock!. I should assume that such bug has been solved long ago. ... ... Are you proposing that we must write code for long ago broken shells?.
– user79743
May 30 '16 at 1:43
@StéphaneChazelas Also: The question title is very specific: Bash.
– user79743
May 30 '16 at 1:46
binary zebra???
– mikeserv
Oct 16 '18 at 19:12
add a comment |
This simple line works (and works on most POSIX shells):
${var+"false"} && echo "var is unset"
Or, written in a longer form:
unset var
if ${var+"false"}
then
echo "var is unset"
fi
The expansion is:
- If the var has a value (even null), false is replaced
- If the var has "no value", then "no value" (null) is replaced.
The ${var+"false"} expansion expands to either "null" of "false".
Then, "nothing" or the "false" is executed, and the exit code set.
There is no need to call the command test ([ or [[) as the exit value is set by the (execution of) the expansion itself.
Yes, except in some old versions of zsh in sh emulation when$IFScontains f, a, l, s or e. Like for other answers, there's the case of arrays, hashes, or other types of variables which one may want to mention.
– Stéphane Chazelas
Dec 1 '15 at 21:00
@StéphaneChazelas I wrotemost POSIX shells.mostmeansIn the greatest number of instances, not all. ... ... So, yes, in an obscure conditionwhen $IFS contains f, a, l, s or eand for some obscure shellsome old versions of zshthis fails: What a shock!. I should assume that such bug has been solved long ago. ... ... Are you proposing that we must write code for long ago broken shells?.
– user79743
May 30 '16 at 1:43
@StéphaneChazelas Also: The question title is very specific: Bash.
– user79743
May 30 '16 at 1:46
binary zebra???
– mikeserv
Oct 16 '18 at 19:12
add a comment |
This simple line works (and works on most POSIX shells):
${var+"false"} && echo "var is unset"
Or, written in a longer form:
unset var
if ${var+"false"}
then
echo "var is unset"
fi
The expansion is:
- If the var has a value (even null), false is replaced
- If the var has "no value", then "no value" (null) is replaced.
The ${var+"false"} expansion expands to either "null" of "false".
Then, "nothing" or the "false" is executed, and the exit code set.
There is no need to call the command test ([ or [[) as the exit value is set by the (execution of) the expansion itself.
This simple line works (and works on most POSIX shells):
${var+"false"} && echo "var is unset"
Or, written in a longer form:
unset var
if ${var+"false"}
then
echo "var is unset"
fi
The expansion is:
- If the var has a value (even null), false is replaced
- If the var has "no value", then "no value" (null) is replaced.
The ${var+"false"} expansion expands to either "null" of "false".
Then, "nothing" or the "false" is executed, and the exit code set.
There is no need to call the command test ([ or [[) as the exit value is set by the (execution of) the expansion itself.
edited Dec 1 '15 at 20:42
answered Dec 1 '15 at 1:11
user79743
Yes, except in some old versions of zsh in sh emulation when$IFScontains f, a, l, s or e. Like for other answers, there's the case of arrays, hashes, or other types of variables which one may want to mention.
– Stéphane Chazelas
Dec 1 '15 at 21:00
@StéphaneChazelas I wrotemost POSIX shells.mostmeansIn the greatest number of instances, not all. ... ... So, yes, in an obscure conditionwhen $IFS contains f, a, l, s or eand for some obscure shellsome old versions of zshthis fails: What a shock!. I should assume that such bug has been solved long ago. ... ... Are you proposing that we must write code for long ago broken shells?.
– user79743
May 30 '16 at 1:43
@StéphaneChazelas Also: The question title is very specific: Bash.
– user79743
May 30 '16 at 1:46
binary zebra???
– mikeserv
Oct 16 '18 at 19:12
add a comment |
Yes, except in some old versions of zsh in sh emulation when$IFScontains f, a, l, s or e. Like for other answers, there's the case of arrays, hashes, or other types of variables which one may want to mention.
– Stéphane Chazelas
Dec 1 '15 at 21:00
@StéphaneChazelas I wrotemost POSIX shells.mostmeansIn the greatest number of instances, not all. ... ... So, yes, in an obscure conditionwhen $IFS contains f, a, l, s or eand for some obscure shellsome old versions of zshthis fails: What a shock!. I should assume that such bug has been solved long ago. ... ... Are you proposing that we must write code for long ago broken shells?.
– user79743
May 30 '16 at 1:43
@StéphaneChazelas Also: The question title is very specific: Bash.
– user79743
May 30 '16 at 1:46
binary zebra???
– mikeserv
Oct 16 '18 at 19:12
Yes, except in some old versions of zsh in sh emulation when
$IFS contains f, a, l, s or e. Like for other answers, there's the case of arrays, hashes, or other types of variables which one may want to mention.– Stéphane Chazelas
Dec 1 '15 at 21:00
Yes, except in some old versions of zsh in sh emulation when
$IFS contains f, a, l, s or e. Like for other answers, there's the case of arrays, hashes, or other types of variables which one may want to mention.– Stéphane Chazelas
Dec 1 '15 at 21:00
@StéphaneChazelas I wrote
most POSIX shells. most means In the greatest number of instances, not all. ... ... So, yes, in an obscure condition when $IFS contains f, a, l, s or e and for some obscure shell some old versions of zsh this fails: What a shock!. I should assume that such bug has been solved long ago. ... ... Are you proposing that we must write code for long ago broken shells?.– user79743
May 30 '16 at 1:43
@StéphaneChazelas I wrote
most POSIX shells. most means In the greatest number of instances, not all. ... ... So, yes, in an obscure condition when $IFS contains f, a, l, s or e and for some obscure shell some old versions of zsh this fails: What a shock!. I should assume that such bug has been solved long ago. ... ... Are you proposing that we must write code for long ago broken shells?.– user79743
May 30 '16 at 1:43
@StéphaneChazelas Also: The question title is very specific: Bash.
– user79743
May 30 '16 at 1:46
@StéphaneChazelas Also: The question title is very specific: Bash.
– user79743
May 30 '16 at 1:46
binary zebra???
– mikeserv
Oct 16 '18 at 19:12
binary zebra???
– mikeserv
Oct 16 '18 at 19:12
add a comment |
The pure shell way:
[ "${var+1}" ] || echo "The variable has not been set"
Test script:
#!/bin/sh
echo "Test 1, var has not yet been created"
[ "${var+1}" ] || echo "The variable has not been set"
echo "Test 2, var=1"
var=1
[ "${var+1}" ] || echo "The variable has not been set"
echo "Test 3, var="
var=
[ "${var+1}" ] || echo "The variable has not been set"
echo "Test 4, unset var"
unset var
[ "${var+1}" ] || echo "The variable has not been set"
echo "Done"
Results:
Test 1, var has not yet been created
The variable has not been set
Test 2, var=1
Test 3, var=
Test 4, unset var
The variable has not been set
Done
1
Fails when variable set to null string.
– Tom Hale
Sep 3 '18 at 14:22
add a comment |
The pure shell way:
[ "${var+1}" ] || echo "The variable has not been set"
Test script:
#!/bin/sh
echo "Test 1, var has not yet been created"
[ "${var+1}" ] || echo "The variable has not been set"
echo "Test 2, var=1"
var=1
[ "${var+1}" ] || echo "The variable has not been set"
echo "Test 3, var="
var=
[ "${var+1}" ] || echo "The variable has not been set"
echo "Test 4, unset var"
unset var
[ "${var+1}" ] || echo "The variable has not been set"
echo "Done"
Results:
Test 1, var has not yet been created
The variable has not been set
Test 2, var=1
Test 3, var=
Test 4, unset var
The variable has not been set
Done
1
Fails when variable set to null string.
– Tom Hale
Sep 3 '18 at 14:22
add a comment |
The pure shell way:
[ "${var+1}" ] || echo "The variable has not been set"
Test script:
#!/bin/sh
echo "Test 1, var has not yet been created"
[ "${var+1}" ] || echo "The variable has not been set"
echo "Test 2, var=1"
var=1
[ "${var+1}" ] || echo "The variable has not been set"
echo "Test 3, var="
var=
[ "${var+1}" ] || echo "The variable has not been set"
echo "Test 4, unset var"
unset var
[ "${var+1}" ] || echo "The variable has not been set"
echo "Done"
Results:
Test 1, var has not yet been created
The variable has not been set
Test 2, var=1
Test 3, var=
Test 4, unset var
The variable has not been set
Done
The pure shell way:
[ "${var+1}" ] || echo "The variable has not been set"
Test script:
#!/bin/sh
echo "Test 1, var has not yet been created"
[ "${var+1}" ] || echo "The variable has not been set"
echo "Test 2, var=1"
var=1
[ "${var+1}" ] || echo "The variable has not been set"
echo "Test 3, var="
var=
[ "${var+1}" ] || echo "The variable has not been set"
echo "Test 4, unset var"
unset var
[ "${var+1}" ] || echo "The variable has not been set"
echo "Done"
Results:
Test 1, var has not yet been created
The variable has not been set
Test 2, var=1
Test 3, var=
Test 4, unset var
The variable has not been set
Done
edited Jan 2 at 8:16
answered Jun 27 '18 at 5:31
Andreas Mikael BankAndreas Mikael Bank
6713
6713
1
Fails when variable set to null string.
– Tom Hale
Sep 3 '18 at 14:22
add a comment |
1
Fails when variable set to null string.
– Tom Hale
Sep 3 '18 at 14:22
1
1
Fails when variable set to null string.
– Tom Hale
Sep 3 '18 at 14:22
Fails when variable set to null string.
– Tom Hale
Sep 3 '18 at 14:22
add a comment |
With bash 4.4.19 the following worked for me. Here is a complete example
$export MAGENTO_DB_HOST="anyvalue"
#!/bin/bash
if [ -z "$MAGENTO_DB_HOST" ]; then
echo "Magento variable not set"
else
echo $MAGENTO_DB_HOST
fi
add a comment |
With bash 4.4.19 the following worked for me. Here is a complete example
$export MAGENTO_DB_HOST="anyvalue"
#!/bin/bash
if [ -z "$MAGENTO_DB_HOST" ]; then
echo "Magento variable not set"
else
echo $MAGENTO_DB_HOST
fi
add a comment |
With bash 4.4.19 the following worked for me. Here is a complete example
$export MAGENTO_DB_HOST="anyvalue"
#!/bin/bash
if [ -z "$MAGENTO_DB_HOST" ]; then
echo "Magento variable not set"
else
echo $MAGENTO_DB_HOST
fi
With bash 4.4.19 the following worked for me. Here is a complete example
$export MAGENTO_DB_HOST="anyvalue"
#!/bin/bash
if [ -z "$MAGENTO_DB_HOST" ]; then
echo "Magento variable not set"
else
echo $MAGENTO_DB_HOST
fi
answered 57 mins ago
Aftab NaveedAftab Naveed
1083
1083
add a comment |
add a comment |
if set|grep '^somevar=' >/dev/null;then
echo "somevar exists"
else
echo "does not exist"
fi
add a comment |
if set|grep '^somevar=' >/dev/null;then
echo "somevar exists"
else
echo "does not exist"
fi
add a comment |
if set|grep '^somevar=' >/dev/null;then
echo "somevar exists"
else
echo "does not exist"
fi
if set|grep '^somevar=' >/dev/null;then
echo "somevar exists"
else
echo "does not exist"
fi
edited Jun 25 '15 at 17:02
cuonglm
106k25213310
106k25213310
answered Jun 25 '15 at 17:01
MelMel
52226
52226
add a comment |
add a comment |
You can't use if command to check the existence of declared variables in bash however -v option exists in newer bash, but it's not portable and you can't use it in older bash versions. Because when you are using a variable if it doesn't exists it will born at the same time.
E.g. Imagine that I didn't use or assign a value to the MYTEST variable, but when you are using echo command it shows you nothing! Or if you are using if [ -z $MYTEST ] it returned zero value!
It didn't return another exit status, which tells you that this variable doesn't exist!
Now you have two solutions (Without -v option):
- Using
declarecommand. - Using
setcommand.
For example:
MYTEST=2
set | grep MYTEST
declare | grep MYTEST
But unfortunately these commands shows you loaded functions in memory too! You can use declare -p | grep -q MYTEST ; echo $? command for cleaner result.
add a comment |
You can't use if command to check the existence of declared variables in bash however -v option exists in newer bash, but it's not portable and you can't use it in older bash versions. Because when you are using a variable if it doesn't exists it will born at the same time.
E.g. Imagine that I didn't use or assign a value to the MYTEST variable, but when you are using echo command it shows you nothing! Or if you are using if [ -z $MYTEST ] it returned zero value!
It didn't return another exit status, which tells you that this variable doesn't exist!
Now you have two solutions (Without -v option):
- Using
declarecommand. - Using
setcommand.
For example:
MYTEST=2
set | grep MYTEST
declare | grep MYTEST
But unfortunately these commands shows you loaded functions in memory too! You can use declare -p | grep -q MYTEST ; echo $? command for cleaner result.
add a comment |
You can't use if command to check the existence of declared variables in bash however -v option exists in newer bash, but it's not portable and you can't use it in older bash versions. Because when you are using a variable if it doesn't exists it will born at the same time.
E.g. Imagine that I didn't use or assign a value to the MYTEST variable, but when you are using echo command it shows you nothing! Or if you are using if [ -z $MYTEST ] it returned zero value!
It didn't return another exit status, which tells you that this variable doesn't exist!
Now you have two solutions (Without -v option):
- Using
declarecommand. - Using
setcommand.
For example:
MYTEST=2
set | grep MYTEST
declare | grep MYTEST
But unfortunately these commands shows you loaded functions in memory too! You can use declare -p | grep -q MYTEST ; echo $? command for cleaner result.
You can't use if command to check the existence of declared variables in bash however -v option exists in newer bash, but it's not portable and you can't use it in older bash versions. Because when you are using a variable if it doesn't exists it will born at the same time.
E.g. Imagine that I didn't use or assign a value to the MYTEST variable, but when you are using echo command it shows you nothing! Or if you are using if [ -z $MYTEST ] it returned zero value!
It didn't return another exit status, which tells you that this variable doesn't exist!
Now you have two solutions (Without -v option):
- Using
declarecommand. - Using
setcommand.
For example:
MYTEST=2
set | grep MYTEST
declare | grep MYTEST
But unfortunately these commands shows you loaded functions in memory too! You can use declare -p | grep -q MYTEST ; echo $? command for cleaner result.
edited Jun 25 '15 at 21:07
Interesting...
1,67531218
1,67531218
answered Jun 25 '15 at 16:43
Sepahrad SalourSepahrad Salour
1,96131424
1,96131424
add a comment |
add a comment |
Function to check if variable is declared/unset
including empty $array=()
In addition to @Gilles's answer
case " ${!foobar*} " in
*" foobar "*) echo "foobar is declared";;
*) echo "foobar is not declared";;
esac
-- which I did not find a way for to encapsulate it within a function -- I'd like to add a simple version, which is partly based on Richard Hansen's answer, but does address also the pitfall that occurs with an empty array=():
# The first parameter needs to be the name of the variable to be checked.
# (See example below)
var_is_declared() {
{ [[ -n ${!1+anything} ]] || declare -p $1 &>/dev/null;}
}
var_is_unset() {
{ [[ -z ${!1+anything} ]] && ! declare -p $1 &>/dev/null;}
}
- By first testing if the variable is (un)set, the call to declare can be avoided, if not necessary.
- If however
$1contains the name of an empty$array=(), the call to declare would make sure we get the right result - There's never much data passed to /dev/null as declare is only called if either the variable is unset or an empty array.
With the following code the functions can be tested:
( # start a subshell to encapsulate functions/vars for easy copy-paste into the terminal
# do not use this extra parenthesis () in a script!
var_is_declared() {
{ [[ -n ${!1+anything} ]] || declare -p $1 &>/dev/null;}
}
var_is_unset() {
{ [[ -z ${!1+anything} ]] && ! declare -p $1 &>/dev/null;}
}
:; echo -n 'a; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a=; echo -n 'a=; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a="sd"; echo -n 'a="sd"; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a=(); echo -n 'a=(); '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a=(""); echo -n 'a=(""); '; var_is_declared a && echo "# is declared" || echo "# is not declared"
unset a; echo -n 'unset a; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
echo ;
:; echo -n 'a; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a=; echo -n 'a=; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a="foo"; echo -n 'a="foo"; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a=(); echo -n 'a=(); '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a=(""); echo -n 'a=(""); '; var_is_unset a && echo "# is unset" || echo "# is not unset"
unset a; echo -n 'unset a; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
)
The script should return
a; # is not declared
a=; # is declared
a="foo"; # is declared
a=(); # is declared
a=(""); # is declared
unset a; # is not declared
a; # is unset
a=; # is not unset
a="foo"; # is not unset
a=(); # is not unset
a=(""); # is not unset
unset a; # is unset
add a comment |
Function to check if variable is declared/unset
including empty $array=()
In addition to @Gilles's answer
case " ${!foobar*} " in
*" foobar "*) echo "foobar is declared";;
*) echo "foobar is not declared";;
esac
-- which I did not find a way for to encapsulate it within a function -- I'd like to add a simple version, which is partly based on Richard Hansen's answer, but does address also the pitfall that occurs with an empty array=():
# The first parameter needs to be the name of the variable to be checked.
# (See example below)
var_is_declared() {
{ [[ -n ${!1+anything} ]] || declare -p $1 &>/dev/null;}
}
var_is_unset() {
{ [[ -z ${!1+anything} ]] && ! declare -p $1 &>/dev/null;}
}
- By first testing if the variable is (un)set, the call to declare can be avoided, if not necessary.
- If however
$1contains the name of an empty$array=(), the call to declare would make sure we get the right result - There's never much data passed to /dev/null as declare is only called if either the variable is unset or an empty array.
With the following code the functions can be tested:
( # start a subshell to encapsulate functions/vars for easy copy-paste into the terminal
# do not use this extra parenthesis () in a script!
var_is_declared() {
{ [[ -n ${!1+anything} ]] || declare -p $1 &>/dev/null;}
}
var_is_unset() {
{ [[ -z ${!1+anything} ]] && ! declare -p $1 &>/dev/null;}
}
:; echo -n 'a; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a=; echo -n 'a=; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a="sd"; echo -n 'a="sd"; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a=(); echo -n 'a=(); '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a=(""); echo -n 'a=(""); '; var_is_declared a && echo "# is declared" || echo "# is not declared"
unset a; echo -n 'unset a; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
echo ;
:; echo -n 'a; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a=; echo -n 'a=; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a="foo"; echo -n 'a="foo"; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a=(); echo -n 'a=(); '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a=(""); echo -n 'a=(""); '; var_is_unset a && echo "# is unset" || echo "# is not unset"
unset a; echo -n 'unset a; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
)
The script should return
a; # is not declared
a=; # is declared
a="foo"; # is declared
a=(); # is declared
a=(""); # is declared
unset a; # is not declared
a; # is unset
a=; # is not unset
a="foo"; # is not unset
a=(); # is not unset
a=(""); # is not unset
unset a; # is unset
add a comment |
Function to check if variable is declared/unset
including empty $array=()
In addition to @Gilles's answer
case " ${!foobar*} " in
*" foobar "*) echo "foobar is declared";;
*) echo "foobar is not declared";;
esac
-- which I did not find a way for to encapsulate it within a function -- I'd like to add a simple version, which is partly based on Richard Hansen's answer, but does address also the pitfall that occurs with an empty array=():
# The first parameter needs to be the name of the variable to be checked.
# (See example below)
var_is_declared() {
{ [[ -n ${!1+anything} ]] || declare -p $1 &>/dev/null;}
}
var_is_unset() {
{ [[ -z ${!1+anything} ]] && ! declare -p $1 &>/dev/null;}
}
- By first testing if the variable is (un)set, the call to declare can be avoided, if not necessary.
- If however
$1contains the name of an empty$array=(), the call to declare would make sure we get the right result - There's never much data passed to /dev/null as declare is only called if either the variable is unset or an empty array.
With the following code the functions can be tested:
( # start a subshell to encapsulate functions/vars for easy copy-paste into the terminal
# do not use this extra parenthesis () in a script!
var_is_declared() {
{ [[ -n ${!1+anything} ]] || declare -p $1 &>/dev/null;}
}
var_is_unset() {
{ [[ -z ${!1+anything} ]] && ! declare -p $1 &>/dev/null;}
}
:; echo -n 'a; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a=; echo -n 'a=; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a="sd"; echo -n 'a="sd"; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a=(); echo -n 'a=(); '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a=(""); echo -n 'a=(""); '; var_is_declared a && echo "# is declared" || echo "# is not declared"
unset a; echo -n 'unset a; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
echo ;
:; echo -n 'a; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a=; echo -n 'a=; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a="foo"; echo -n 'a="foo"; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a=(); echo -n 'a=(); '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a=(""); echo -n 'a=(""); '; var_is_unset a && echo "# is unset" || echo "# is not unset"
unset a; echo -n 'unset a; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
)
The script should return
a; # is not declared
a=; # is declared
a="foo"; # is declared
a=(); # is declared
a=(""); # is declared
unset a; # is not declared
a; # is unset
a=; # is not unset
a="foo"; # is not unset
a=(); # is not unset
a=(""); # is not unset
unset a; # is unset
Function to check if variable is declared/unset
including empty $array=()
In addition to @Gilles's answer
case " ${!foobar*} " in
*" foobar "*) echo "foobar is declared";;
*) echo "foobar is not declared";;
esac
-- which I did not find a way for to encapsulate it within a function -- I'd like to add a simple version, which is partly based on Richard Hansen's answer, but does address also the pitfall that occurs with an empty array=():
# The first parameter needs to be the name of the variable to be checked.
# (See example below)
var_is_declared() {
{ [[ -n ${!1+anything} ]] || declare -p $1 &>/dev/null;}
}
var_is_unset() {
{ [[ -z ${!1+anything} ]] && ! declare -p $1 &>/dev/null;}
}
- By first testing if the variable is (un)set, the call to declare can be avoided, if not necessary.
- If however
$1contains the name of an empty$array=(), the call to declare would make sure we get the right result - There's never much data passed to /dev/null as declare is only called if either the variable is unset or an empty array.
With the following code the functions can be tested:
( # start a subshell to encapsulate functions/vars for easy copy-paste into the terminal
# do not use this extra parenthesis () in a script!
var_is_declared() {
{ [[ -n ${!1+anything} ]] || declare -p $1 &>/dev/null;}
}
var_is_unset() {
{ [[ -z ${!1+anything} ]] && ! declare -p $1 &>/dev/null;}
}
:; echo -n 'a; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a=; echo -n 'a=; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a="sd"; echo -n 'a="sd"; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a=(); echo -n 'a=(); '; var_is_declared a && echo "# is declared" || echo "# is not declared"
a=(""); echo -n 'a=(""); '; var_is_declared a && echo "# is declared" || echo "# is not declared"
unset a; echo -n 'unset a; '; var_is_declared a && echo "# is declared" || echo "# is not declared"
echo ;
:; echo -n 'a; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a=; echo -n 'a=; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a="foo"; echo -n 'a="foo"; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a=(); echo -n 'a=(); '; var_is_unset a && echo "# is unset" || echo "# is not unset"
a=(""); echo -n 'a=(""); '; var_is_unset a && echo "# is unset" || echo "# is not unset"
unset a; echo -n 'unset a; '; var_is_unset a && echo "# is unset" || echo "# is not unset"
)
The script should return
a; # is not declared
a=; # is declared
a="foo"; # is declared
a=(); # is declared
a=(""); # is declared
unset a; # is not declared
a; # is unset
a=; # is not unset
a="foo"; # is not unset
a=(); # is not unset
a=(""); # is not unset
unset a; # is unset
edited Apr 13 '17 at 12:36
Community♦
1
1
answered Mar 11 '16 at 21:48
Martin RüeggMartin Rüegg
1013
1013
add a comment |
add a comment |
bash function that works for both scalar and array types:
definition
has_declare() { # check if variable is set at all
local "$@" # inject 'name' argument in local scope
&>/dev/null declare -p "$name" # return 0 when var is present
}
invocation
if has_declare name="vars_name" ; then
echo "variable present: vars_name=$vars_name"
fi
add a comment |
bash function that works for both scalar and array types:
definition
has_declare() { # check if variable is set at all
local "$@" # inject 'name' argument in local scope
&>/dev/null declare -p "$name" # return 0 when var is present
}
invocation
if has_declare name="vars_name" ; then
echo "variable present: vars_name=$vars_name"
fi
add a comment |
bash function that works for both scalar and array types:
definition
has_declare() { # check if variable is set at all
local "$@" # inject 'name' argument in local scope
&>/dev/null declare -p "$name" # return 0 when var is present
}
invocation
if has_declare name="vars_name" ; then
echo "variable present: vars_name=$vars_name"
fi
bash function that works for both scalar and array types:
definition
has_declare() { # check if variable is set at all
local "$@" # inject 'name' argument in local scope
&>/dev/null declare -p "$name" # return 0 when var is present
}
invocation
if has_declare name="vars_name" ; then
echo "variable present: vars_name=$vars_name"
fi
edited Oct 22 '17 at 2:39
answered Oct 22 '17 at 1:36
Andrei PozolotinAndrei Pozolotin
18926
18926
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f212183%2fhow-do-i-check-if-a-variable-exists-in-an-if-statement%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you want to set
$somevarto a value/string if variable does not exist:${somevar:=42}.– Cyrus
Jun 25 '15 at 18:49
Personally, I tend to check just for emptiness (
[ -n "$var" ]or[ ! -z "$var" ]). I think the existence/nonexistence checks are too subtle, and I prefer my code coarse and simple.– PSkocik
Dec 1 '15 at 17:12
Why do you need that vs
[ -n "$var" ]? Related: stackoverflow.com/questions/3601515/…– Ciro Santilli 新疆改造中心996ICU六四事件
Jun 28 '16 at 10:36