for example, the variable is like:
ANY=' ab1 de$% '
The middle part 'ab1 de$%' can be any combination of characters.
I want to get the substring of leading whitespaces, to indent other lines.
You can use search-and-replace parameter substitution:
leading_whitespace="${ANY/[^[:space:]]*/}"
EDIT:
Or also substring-removal parameter substitution as mentioned by David C. Rankin below:
leading_whitespace="${ANY%%[![:space:]]*}"
[^[:space:]]* might be closer to the problem specification.lws="${any%%[![:space:]]*}"You can use parameter expansion of ${parameter%%word}.
leading="${ANY%%[[:graph:]]*}"
(Edited) or more restrictively:
leading="${ANY%%[^[:space:]]*}"
Depending on circumstances, trimming ONLY whitespace characters might be preferred, whereas in other circumstances, trimming all non-graphical characters might be better. Consider the BEL, BS, and DEL characters. See https://www.regular-expressions.info/posixbrackets.html for the subtle difference.
lws="${ANY%%[![:space:]]*}" for the reason mentioned by rici above – in that is more closely matches the problem description.
graphcharacters (e.g.ab1de$%)? or also the trailing whitespace?