Bash – Assign the same string to multiple variables

assignmentbash

I want to assign the string contained in $value to multiple variables.

Actually the example I gave before (var1=var2=...=$value) was not reflecting exactly what I wanted.
So far, I found this but it only works if $value is an integer:

$ let varT=varz=var3=$value

How can I do that?

Best Answer

In my opinion you're better of just doing the more readable:

var1="$value" var2="$value" var3="$value" var4="$value" var5="$value" var6="$value" var7="$value" var8="$value" var9="$value" var10="$value"

But if you want a very short way of accomplishing this then try:

declare var{1..10}="$value"

Edited: using brace expansions instead of printf and declare instead of eval, which could be dangerous depending on what's in $value.

Cf. EDIT1: You could still use brace expansions in the new case:

declare var{T,z,3}="$value"

It's safer than the printf approach in the comments because it can handle spaces in $value.

Related Question