Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable containing spaces 2

Status
Not open for further replies.

marielc

Programmer
Feb 24, 2004
2
GB
Hi,
Does anyone know how I can test a variable for only containing spaces?
 
Try something like this:
Code:
[ `expr "$var" : ' *'` -eq `expr length "$var"` ] &&
  echo "'$var' is empty or only space"

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Another form :

[tt]
[ `expr "$var" : " \+$" >/dev/null` ] && "Only spaces"
[/tt]

Jean Pierre.
 
By the moment a variable full of space is an empty var a very simple test is:
a=' '
if [ X$a = "X" ];then
echo "var \$a is empty "
fi
 
Sorry sbix, your solution doesn,t works when the variable contains special characters or text with space(s) :
[tt]
home/jp> a="Not Only Spaces"
home/jp> [ X$a = "X" ] && echo empty
/bin/ksh: Only: unknown test operator
home/jp>
[/tt]

Another method with ksh :
[tt]
home/jp> a=xxxxx
home/jp> [[ "$a" = +( ) ]] && echo "Only spaces"
home/jp> a=" "
home/jp> [[ "$a" = +( ) ]] && echo "Only spaces"
Only spaces
home/jp>
[/tt]

Jean Pierre.
 
Infact the question was "Does anyone know how I can test a variable for ONLY containing spaces?"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top