Skip to main content
Tweeted twitter.com/StackUnix/status/1122470708934791170
edited tags
Link
Gilles 'SO- stop being evil'
  • 866.1k
  • 205
  • 1.8k
  • 2.3k
deleted 103 characters in body; edited title
Source Link
Mat
  • 54.9k
  • 11
  • 164
  • 143

bash + how to define in the brackets to ignore How can I test if a variable is empty or contains only spaces in param?

Dear colleges and friends

As all know theThe following bash syntax verifyverifies if pram isnt zeroparam isn't empty:

 [[ ! -z $param ]] 

For example:

param=””param="" [[ ! -z $param ]] && echo “ I"I am not zero ” No output and its finezero" 

No output and its fine.

But when param have evenparam is empty except for one (or more) space characters, then the case is different:

param=” ” param=" <--" # one space [[ ! -z $param ]] && echo “I am not zero ” I"I am not zerozero" 

So my question"I am not zero" is how to define in the brackets to ignore empty spaces (empty spaces will defined as zero )output.

soHow can I change the test to consider variables that contain only in case $param have one character or more then it will be non zero casespace characters as empty?

bash + how to define in the brackets to ignore empty spaces in param

Dear colleges and friends

As all know the following bash syntax verify if pram isnt zero

 [[ ! -z $param ]] 

For example

param=”” [[ ! -z $param ]] && echo “ I am not zero ” No output and its fine 

But when param have even empty one space then the case is different

param=” ”  <-- one space [[ ! -z $param ]] && echo “I am not zero ” I am not zero 

So my question is how to define in the brackets to ignore empty spaces (empty spaces will defined as zero )

so only in case $param have one character or more then it will be non zero case

How can I test if a variable is empty or contains only spaces?

The following bash syntax verifies if param isn't empty:

 [[ ! -z $param ]] 

For example:

param="" [[ ! -z $param ]] && echo "I am not zero" 

No output and its fine.

But when param is empty except for one (or more) space characters, then the case is different:

param=" " # one space [[ ! -z $param ]] && echo "I am not zero" 

"I am not zero" is output.

How can I change the test to consider variables that contain only space characters as empty?

Source Link
maihabunash
  • 7.2k
  • 22
  • 68
  • 80

bash + how to define in the brackets to ignore empty spaces in param

Dear colleges and friends

As all know the following bash syntax verify if pram isnt zero

 [[ ! -z $param ]] 

For example

param=”” [[ ! -z $param ]] && echo “ I am not zero ” No output and its fine 

But when param have even empty one space then the case is different

param=” ” <-- one space [[ ! -z $param ]] && echo “I am not zero ” I am not zero 

So my question is how to define in the brackets to ignore empty spaces (empty spaces will defined as zero )

so only in case $param have one character or more then it will be non zero case