1

I found a bit of code here that can be used to convert bases in bash neatly. How do I use it and get the result into a bash variable?

I tried something like below, but it did not work.

NUM_IN_DEC=12321
BASE36=($(echo {0..9} {a..z}))
NAME=(
    for i in $(bc <<< "obase=36; $NUM_IN_DEC"); do
        echo -n ${BASE36[$(( 10#$i ))]};
    done && echo
)
echo converted value is $NAME
0

1 Answer 1

3

Just use command substitution to set NAME

NUM_IN_DEC=12321
BASE36=($(echo {0..9} {a..z}))
NAME=$(
    for i in $(bc <<< "obase=36; $NUM_IN_DEC"); do
        echo -n ${BASE36[$(( 10#$i ))]};
    done && echo
)
echo converted value is $NAME
9i9
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.