I tried this, but it doesn't work.
if [ $char -eq " " -o $char -eq "" -o $char -eq " " ]
I tried this, but it doesn't work.
if [ $char -eq " " -o $char -eq "" -o $char -eq " " ]
-eq for numeric value, for string, use ==
if [[ $char == " " || -z $char || $char == " " ]]; then
regex alternative with recent bash ( 0 or 1 space or 3 spaces ):
if [[ $char =~ ^\ {0,1}$ || $char =~ ^\ {3}$ ]] ; then
\ : space only[[:blank:]] : space, tab[[:space:]] : space, tab, newline, vertical tab, form feed, carriage returnif [[ $char =~ ^\ {0,3}$ ]] ; then In bash you can use regex match in [[]]
if [[ $char =~ [[:space:]] ]]
If you actually want to match a single whitespace character
if [[ $char =~ ^[[:space:]]$ ]]
Or single whitespace or nothing
if [[ $char =~ ^[[:space:]]?$ ]]
Or only whitespace in a string
if [[ $char =~ ^[[:space:]]+$ ]]
Or only whitespace or nothing
if [[ $char =~ ^[[:space:]]*$ ]]
char='a a' matches...[[:space:]] is a character class for whitespace. Note that the code in the question is just what OP tried, not necessarily exactly what they want.