Answer by rogerdpack for How to determine if a bash variable is empty?
Not an exact answer, but ran into this trick. If the string you're looking for comes from "a command" then you can actually store the command in an env. variable and then execute it every time for the...
View ArticleAnswer by gluk47 for How to determine if a bash variable is empty?
My 5 cents: there is also a shorter syntax than if ..., this one: VALUE="${1?"Usage: $0 value"}" This line will set VALUE if an argument has been supplied and will print an error message prepended with...
View ArticleAnswer by Luca Borrione for How to determine if a bash variable is empty?
The question asks how to check if a variable is an empty string and the best answers are already given for that. But I landed here after a period passed programming in php and what I was actually...
View ArticleAnswer by andrej for How to determine if a bash variable is empty?
oneliner extension of duffbeer703's solution: #! /bin/bash [ -z "$1" ] || some_command_that_needs_$1_parameter
View ArticleAnswer by Richard Hansen for How to determine if a bash variable is empty?
A variable in bash (and any POSIX-compatible shell) can be in one of three states: unset set to the empty string set to a non-empty string Most of the time you only need to know if a variable is set to...
View ArticleAnswer by Dennis Williamson for How to determine if a bash variable is empty?
In Bash, when you're not concerned with portability to shells that don't support it, you should always use the double-bracket syntax: Any of the following: if [[ -z $variable ]] if [[ -z "$variable" ]]...
View ArticleAnswer by Fedir RYKHTIK for How to determine if a bash variable is empty?
Personally prefer more clear way to check : if [ "${VARIABLE}" == "" ]; then echo VARIABLE is empty else echo VARIABLE is not empty fi
View ArticleAnswer by errant.info for How to determine if a bash variable is empty?
An alternate I've seen to [ -z "$foo" ] is the following, however I'm not sure why people use this method, anyone know? [ "x${foo}" = "x" ] Anyway if you're disallowing unset variables (either by set...
View ArticleAnswer by JHS for How to determine if a bash variable is empty?
This is true exactly when $FOO is set and empty: [ "${FOO+x}" = x ] && [ -z "$FOO" ]
View ArticleAnswer by namewithoutwords for How to determine if a bash variable is empty?
the entire if-then and -z are unnecessary. [ "$foo" ] && echo "foo is not empty" [ "$foo" ] || echo "foo is indeed empty"
View ArticleAnswer by Rory for How to determine if a bash variable is empty?
-z is a the best way. Another options I've used is to set a variable, but it can be overridden by another variable eg export PORT=${MY_PORT:-5432} If the $MY_PORT variable is empty, then PORT gets set...
View ArticleAnswer by MikeyB for How to determine if a bash variable is empty?
If you're interested in distinguishing the cases of set-empty versus unset status, look at the -u option for bash: $ set -u $ echo $BAR bash: BAR: unbound variable $ [ -z "$BAR" ] && echo true...
View ArticleAnswer by duffbeer703 for How to determine if a bash variable is empty?
This will return true if a variable is unset or set to the empty string (""). if [ -z "$VAR" ];
View ArticleHow to determine if a bash variable is empty?
What is the best way to determine if a variable in bash is empty ("")? I have heard that it is recommended that I do if [ "x$variable" = "x" ] Is that the correct way? (there must be something more...
View ArticleAnswer by mgutt for How to determine if a bash variable is empty?
I prefer the way how PHP checks for an empty variable as Luca mentioned in his answer, too. But instead of using a separate function, I use a "filter" which finally allows using usual bash...
View ArticleAnswer by Raman Kathpalia for How to determine if a bash variable is empty?
To figure out if a variable "Foo" is empty and also contains no spaces (or no whitespace as some people refer it). if [[ -n "${Foo/[ ]*\n/}" ]];then echo "Foo is not empty and contains non space...
View Article