1

I have a string variable which could have values like below.

my_string=" "
my_string="   "
my_string="name1"
my_string="name 2"

I have to identify if my_string has only spaces/whitespaces and exit the program when it has only whitespaces. If it has one or two spaces inbetween, it's a valid string.

How to check if the string has only whitespaces and exit the shell script based on that.

Thank you.

8
  • "Shell" meaning /bin/sh or bash? (bash has built-in support for extended regular expressions) Commented Mar 30, 2022 at 18:58
  • (also, what do you mean "between"? Between individual words in the string? Between the start and end of the string?) Commented Mar 30, 2022 at 18:59
  • ...have you tried to follow the advice given in preexisting questions like Check if a string matches a regex in bash script or Count occurances of a char in a string using bash? If so, where did you get stuck? Commented Mar 30, 2022 at 19:01
  • /bin/sh. Between start and end of string Commented Mar 30, 2022 at 19:08
  • If you want to do pattern matches in /bin/sh, the case statement is your friend. It's not as powerful as a real regex engine, but if you don't want to use grep, it's what you have (and it's often powerful enough). I can't speak beyond that because I still don't understand your requirements in any detail. Commented Mar 30, 2022 at 19:21

2 Answers 2

3

Your question is somewhat unclear. However the following example should point you in the right direction.

#!/bin/sh

#my_string=""
my_string="     "
#my_string="   "
#my_string="name1"
#my_string="name 2"

case "$my_string" in
  "")             echo "string is empty";;
  *[![:space:]]*) echo "string does not contain only whitespace";;
  *)              echo "string contains only whitespace"; exit 1;;
esac

If you want to test only for spaces and tabs rather than all whitespace, you should use [:blank:] instead of [:space:].

Sign up to request clarification or add additional context in comments.

6 Comments

Nice use of case. Since your interpreter is bash, for bonus points the example could be my_array=( "" " " " " "name1" "name 2"); for my_string in "${my_array[@]}"; do case ... done. Not a critique, just a tip.
Good solution, but requires bash, while the OP is looking for a POSIX shell solution.
@user1934428 What part of the case conditional construct is non-POSIX?
I thought it was the [![:space:]] glob pattern. Or is this already present in POSIX?
@user1934428. Err, glob is a pathname generator that implements the rules defined in the Shell and Utilities volume of POSIX.1-2017, Section 2.13, Pattern Matching Notation, with optional support for rule 3 in the Shell and Utilities volume of POSIX.1-2017, Section 2.13.3, Patterns Used for Filename Expansion.
|
0

Since you are searching for a solution in POSIX shell, I would do something like this:

if [ "$(echo $my_string | tr -d ' ')" = '' ]
then
  echo The string is blank
fi

2 Comments

This is very inefficient as it requires the use of tr which is not a shell builtin. Your example also fails for a tab. POSIX regards a tab as whitespace!
Right, I was thinking of space characters only. Even worse, I now notice that the OP said: If it has one or two spaces inbetween, it's a valid string. This makes me wonder whether a b is considered valid or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.