I have tried to execute the code below on my Ubuntu machine, but it keeps saying "syntax error in conditional expression" and "syntax error near '0-9'" can you tell me why?
While ! [[ $num =~ ^[ 0-9 ] +$ && $num = 0 ]] ; do
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
Your while shouldn't be capitalized but I think that only happened when typing into this question otherwise you would get a different error.
Your issue is likely just that your regular expression is wrong.
[ 0-9 ] should be
[0-9] with no spaces. Once you fix that however you will get a different error about
+$ because you will also need to escape the space between them.
while ! [[ $num =~ ^[0-9]\ +$ && $num = 0 ]] ; do
It's often better to store patterns like this in a variable for use with the extended test:
pattern='^[0-9] +$' while ! [[ $num =~ $pattern && $num -eq 0 ]]; do