1

I have an array of numbers 0 10 20 30 40 and I am trying to add a string to the front the array so that the output will look like this: 1D: 0 10 20 30 40

I've tried coding it this way:

string="1D: "
new=( $(( $string + ${array[@]} )) )
echo $new

Which gives me this error:

-bash: 1D: value too great for base (error token is "1D")

Can someone assist me in this problem?

Thanks!

2 Answers 2

2

You can use:

array=(0 10 20 30 40)
string="1D:"
new=("$string" "${array[@]}")

Check new array:

declare -p new

declare -a new='([0]="1D:" [1]="0" [2]="10" [3]="20" [4]="30" [5]="40")'

Or else:

echo "${new[@]}"
1D: 0 10 20 30 40
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @anubhava. Is there also a way to make this into a string? This array is meant to be used as input for a flag option in some software I'm using. Normally the line of code would look like -concat '1D: 0 10 20 30 40', but I'm trying to do it like this -concat '$new' however the software is giving me an error there. Also, I've adjusted my variable so that it's new=echo ${new[@]}
@djl: That looks like a different problem. You may please post a new question and we'll try to help you out.
0

If you need to prepend a string to an array you can do something like this

array=("0" "10" "20" "30" "40")
array=("1D:" "${array[@]}")
echo ${array[@]}

output will be

1D: 0 10 20 30 40

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.