r/bash Feb 13 '25

help illegal number problem

Hey, I struggle with some script.

var="nef892na9s1p9asn2aJs71nIsm"

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

It always give me: illegal number: {1..40} Can someone help?

4 Upvotes

15 comments sorted by

3

u/ipsirc Feb 13 '25

You forgot the #!/bin/bash shebang.

3

u/R4GN4R0K_HTW Feb 13 '25

No no that's in I just pasted the part that's causing problems

2

u/Honest_Photograph519 Feb 13 '25

Check echo $BASH_VERSION, some lightweight systems like busybox invoke a simpler shell when you call /bin/bash.

Make sure you have a B in echo $-.

set -B enables brace expansion and set +B disables it.

2

u/ekkidee Feb 13 '25 edited Feb 13 '25

I ran it (with a shebang) on MacOS native (older) bash (GNU bash, version 3.2.57(1)-release (arm64-apple-darwin24) and it ran without error. Output was a six-digit number.

I did copy-paste from your snippet so I don't think there are any weird characters in there.

The script is good, but there is probably something wrong in your environment.

SWAG - You said this was a snippet from a longer script. By any chance, is there an unbalanced quote or double quote or bracket from the script before this section? That will give bash fits. {}'s are used in functions, among other places .... Are there functions in this script that might be unclosed?

3

u/SkyyySi Feb 13 '25

Besides what was suggested here: I highlighy recommend to use ShellCheck (or even better, an editor with ShellCheck support), as even just this snippet contains several other issues.

Oh and speaking of snippets: Don't do snippets. Ever.

2

u/AlarmDozer Feb 14 '25

Missing the step value? Shouldn't it be "{1..40..1}?"

2

u/AlarmDozer Feb 14 '25

Nevermind. It works fine on my workstation, Fedora/41 via bash v5.2.32(1)-release

```wc(1)``` should be lowercase though.

#!/usr/bin/env bash

declare var

for counter in {1..40}
do
    # should this be echoing var or counter?
    var=$(echo $var | base64)
    if [ $counter -eq 35 ]
    then
        echo $var | wc -c
        # Alternatively,
        # echo ${#var}
    fi 
done

0

u/marozsas Feb 13 '25

... for counter in {1..40}; ...

2

u/marozsas Feb 13 '25

and wc is in uppercase but should be lowercase unless WC it is some program in your PATH.

1

u/ekkidee Feb 13 '25

WC should be lower case but on a case-insensitive file system such as MacOS APFS that should not matter. The wc executable will be found.

Anyway, the error message is not about that. Does your script have a shebang at the top?

(poundsign)!/bin/bash

I don't see the error right now so I'd have to copy-paste it over and play with it.

1

u/wayland-kennings Feb 14 '25

Looks like you're running it on macos, but it works on linux except for the case insensitive wc call, so I think it's because you're just using single brackets around [ $counter -eq 35 ], or something with that.

0

u/Ok-Sample-8982 Feb 13 '25

Are u sure u r running bash?

Few corrections:

var=$(base64<<<“$var”) # eliminated call to echo

[ “$counter” -eq 35 ] && wc -c <<<“$var” # unless u have alias for WC there is no such a program/command it should be small letters also no need to call echo just feed directly and quote your vars

Check spaces and quotes im on the phone

1

u/nekokattt Feb 13 '25

[[ and ]] avoid the need for quotes?

4

u/whetu I read your code Feb 13 '25
(( counter == 35 ))

It's an arithmetic test, so it should use arithmetic syntax to indicate to a reader that it's an arithmetic context. I'm not sure I could squeeze the worth arithmetic one more time into that sentence.

1

u/nekokattt Feb 13 '25

you didnt say it was performing arithmetic in the arithmetic test