How to check if a variable contains space in it??? or any other character??? using if condition..
unless its empty it will hopefully contain characters... What do you mean by other?
grep " " -c
a="ind ia"
b="india"
here, variabe a contains space, but b dont...
like this, i want to check that the variable contains i or not...
a="ind ia"
b="india"
$ echo $a | grep " " -c
1
$ echo $b | grep " " -c
0
Please post what Operating System you have and what Shell you use.
If you are validating a field, it is better to work from a list of valid characters.
What are the valid characters?
If you just want to count the characters, one trick is to delete everything except that character, then count what's left:
# Counting space characters
echo "abc def"|tr -cd " "|wc -c
1
echo "abcdef"|tr -cd " "|wc -c
0
# Counting "a" characters
echo "abc def"|tr -cd "a"|wc -c
1
echo "abcdef"|tr -cd "a"|wc -c
1
@pamu
The grep -c
command counts lines not characters.