Quantcast
Channel: How to determine if a bash variable is empty? - Server Fault
Browsing latest articles
Browse All 16 View Live

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 Article



Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


How 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 Article

Answer 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 Article


Answer 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
Browsing latest articles
Browse All 16 View Live




Latest Images