5

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?

  • 1
    Shouldn't the question be: Assign the same string value to multiple variables? (subject and object switched)? – rexkogitans May 15 at 9:30
6

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.

  • Please see my EDIT1. The printf method you gave was just fine if you put the declare in front : declare $(printf .....) – SebMa May 15 at 7:56
  • With EDIT1 using printf that would be: declare $(printf "var%s=$value " T z 3). But be careful with spaces in $value. – laenkeio May 15 at 8:04
  • Note that the declare approach is still dangerous if any of the variables were previously declared as arrays. Try after var3=() value='([0$(echo Gotcha>&2)]=)' – Stéphane Chazelas May 15 at 12:01
  • eval is fine if used as eval var{1..10}=\$value, that is as long as the (arbitrary) content of $value is not evaluated as shell code. – Stéphane Chazelas May 15 at 12:19
6

let is doing an arithmetic evaluation. In bash, this is equivalent to (( ... )). This is why your code only works for integers.

Using an array instead of specially named variables:

var=( "$value"  "$value" "$value" "$value" "$value" "$value" "$value" "$value" "$value" "$value" )

printf 'var[5]=%s\n' "${var[5]}"

or an associative array,

declare -A var

var=( ["T"]=$value ["z"]=$value [3]=$value )

printf 'var[T]=%s\n' "${var["T"]}"

You could also do a loop:

for varname in var0 var1 varT varfoo; do
    declare -n var="$varname"
    var=$value
done

This loop loops over names of variables. In the loop body, a name reference variable is created that references the current loop variable. When the value of the name reference variable is set, the named variable is set.

  • What does the -n option do? I get an error with it. Ah... namerefs (>= 4.3, IIRC?) – muru May 15 at 8:08
  • 1
    @muru Yes, declare -n creates a name reference variable. You would have to run bash 4.3+ to use name references. – Kusalananda May 15 at 8:09
3
value=balabala
eval var{1..10}=\$value
echo $var{1..10}
2
Value='-%% this is a test %%-'
for VarName in myVar1 myVar2 myOtherVar; do printf -v "$VarName" %s "$Value"; done
New contributor
Buygrush is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.