9

I want to assign the string contained in $value to multiple BASH 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?

SebMa
  • 1,941
  • 4
  • 22
  • 37
  • 2
    Shouldn't the question be: Assign the same string value to multiple variables? (subject and object switched)? – rexkogitans May 15 '19 at 09:30

5 Answers5

10

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.

laenkeio
  • 581
  • 3
  • 8
  • Please see my EDIT1. The `printf` method you gave was just fine if you put the declare in front : `declare $(printf .....)` – SebMa May 15 '19 at 07: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 '19 at 08: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 '19 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 '19 at 12:19
7

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.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • What does the `-n` option do? I get an error with it. Ah... namerefs (>= 4.3, IIRC?) – muru May 15 '19 at 08: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 '19 at 08:09
3
value=balabala
eval var{1..10}=\$value
echo $var{1..10}
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
dedowsdi
  • 1,008
  • 6
  • 12
2
Value='-%% this is a test %%-'
for VarName in myVar1 myVar2 myOtherVar; do printf -v "$VarName" %s "$Value"; done
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
Buygrush
  • 21
  • 1
1

you could do:

declare {varT,varz}="$value"
Aybak3k
  • 11
  • 2