Help with Illegal Number Error in Bash Script

0
2
Asked By TechyTurtle42 On

I'm having trouble with a portion of my Bash script that results in an 'illegal number' error. Here's the snippet I'm working with:

```bash
var="nef892na9s1p9asn2aJs71nIsm"

for counter in {1..40}
do
var=$(echo $var | base64)
if [ $counter -eq 35 ]
then
echo $var | WC -c
fi
done
```

The error message I keep getting is 'illegal number: {1..40}'. Can someone help me figure out what's wrong?

4 Answers

Answered By BashNinja88 On

Don't forget, the format for your range should be `{1..40..1}`. It's an easy fix, and it might clear up that error. Also, make sure there's no confusion with uppercase `WC`, as it should be lowercase `wc`, unless you're specifically referencing a different command with that name.

TechyTurtle42 -

Thanks! I’ll try changing it to lowercase and see if that helps.

Answered By CodeChaser45 On

I ran a similar version of your script, and it worked fine on my Mac with an older version of Bash. Just to cover all bases, check for any unbalanced quotes or brackets in the parts of the script leading up to this section. That can often trip up Bash.

Answered By DevDoctor73 On

Make sure you're actually running the script in Bash. Sometimes, other shells might lead to compatibility issues with the syntax you're using. You might also want to try using `var=$(base64 <<< "$var")` to eliminate the call to `echo`, which can clean things up a bit.

TechyTurtle42 -

Good point! I need to double-check my shell.

Answered By ScriptSavvy99 On

It looks like you're missing the shebang at the start of your script. Make sure you have `#!/bin/bash` at the very top. It sets the interpreter for your script and can help avoid errors like this one. The rest of your script seems fine, but just ensure you're executing it in the right environment.

TechyTurtle42 -

I do have the shebang! I just pasted the part causing issues.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.