Help with Illegal Number Error in Bash Script

April 20, 2025 4 answers
Asked by not-so-fast-please
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?

Answers

72dpi_preview
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.

not-so-fast-please: I do have the shebang! I just pasted the part causing issues.

Reply to this answer
counterpoint 17
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.

not-so-fast-please: Good point! I need to double-check my shell.

Reply to this answer
pad+thermal
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.
Reply to this answer
calendar_grid_
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.

not-so-fast-please: Thanks! Iโ€™ll try changing it to lowercase and see if that helps.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions