Assign the same string to multiple variablesVariable name from command outputSharing variables across multiple shell scriptsReplace string in a file with another where both are saved in variablesPassing multiple of arguments with whitespaces through a script to sshHow to assign variables associated with list of ranges to another table with integersCut the string in half with the last specific character shows up in the stringIn bash, how to delay interpolation of a variable that has a dynamic variable within its stringHow can I evaluate bash arguments in a string once variables have changedCreating n variables in Bash without assigning them one by one?Escaping a string for a parameter inside a script
If a character has cast the Fly spell on themselves, can they "hand off" to the Levitate spell without interruption?
Wifi light switch needs neutral wire. Why? AND Can that wire be a skinny one?
Make the `diff` command look only for differences from a specified range of lines
Are there historical examples of audiences drawn to a work that was "so bad it's good"?
Is a world with one country feeding everyone possible?
Is there an idiom that means that you are in a very strong negotiation position in a negotiation?
DeleteCases using two lists but with partial match?
Can the Conjure Barrage spell stack with the Disarming Attack or Trip Attack Battle Master maneuvers?
How does the Earth's center produce heat?
Meaning of "half-crown enclosure"
Ratings matrix plot
VHDL: Why is it hard to design a floating point unit in hardware?
Find this Unique UVC Palindrome ( ignoring signs and decimal) from Given Fractional Relationship
What defines a person who is circumcised "of the heart"?
Why do testers need root cause analysis?
Ribbon Cable Cross Talk - Is there a fix after the fact?
Unary Enumeration
Why is Ni[(PPh₃)₂Cl₂] tetrahedral?
amsmath: How can I use the equation numbering and label manually and anywhere?
One word for 'the thing that attracts me'?
Adobe Illustrator: How can I change the profile of a dashed stroke?
How to safely discharge oneself
How to test if argument is a single space?
How do you earn the reader's trust?
Assign the same string to multiple variables
Variable name from command outputSharing variables across multiple shell scriptsReplace string in a file with another where both are saved in variablesPassing multiple of arguments with whitespaces through a script to sshHow to assign variables associated with list of ranges to another table with integersCut the string in half with the last specific character shows up in the stringIn bash, how to delay interpolation of a variable that has a dynamic variable within its stringHow can I evaluate bash arguments in a string once variables have changedCreating n variables in Bash without assigning them one by one?Escaping a string for a parameter inside a script
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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?
bash assignment
add a comment |
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?
bash assignment
1
Shouldn't the question be: Assign the same string value to multiple variables? (subject and object switched)?
– rexkogitans
May 15 at 9:30
add a comment |
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?
bash assignment
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?
bash assignment
bash assignment
edited May 15 at 9:39
muru
38.4k591168
38.4k591168
asked May 15 at 7:06
SebMaSebMa
3351413
3351413
1
Shouldn't the question be: Assign the same string value to multiple variables? (subject and object switched)?
– rexkogitans
May 15 at 9:30
add a comment |
1
Shouldn't the question be: Assign the same string value to multiple variables? (subject and object switched)?
– rexkogitans
May 15 at 9:30
1
1
Shouldn't the question be: Assign the same string value to multiple variables? (subject and object switched)?
– rexkogitans
May 15 at 9:30
Shouldn't the question be: Assign the same string value to multiple variables? (subject and object switched)?
– rexkogitans
May 15 at 9:30
add a comment |
4 Answers
4
active
oldest
votes
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 var1..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 varT,z,3="$value"
It's safer than the printf
approach in the comments because it can handle spaces in $value
.
Please see my EDIT1. Theprintf
method you gave was just fine if you put the declare in front :declare $(printf .....)
– SebMa
May 15 at 7:56
With EDIT1 usingprintf
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 thedeclare
approach is still dangerous if any of the variables were previously declared as arrays. Try aftervar3=() value='([0$(echo Gotcha>&2)]=)'
– Stéphane Chazelas
May 15 at 12:01
eval
is fine if used aseval var1..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
add a comment |
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]=%sn' "$var[5]"
or an associative array,
declare -A var
var=( ["T"]=$value ["z"]=$value [3]=$value )
printf 'var[T]=%sn' "$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 runbash
4.3+ to use name references.
– Kusalananda♦
May 15 at 8:09
add a comment |
value=balabala
eval var1..10=$value
echo $var1..10
add a comment |
Value='-%% this is a test %%-'
for VarName in myVar1 myVar2 myOtherVar; do printf -v "$VarName" %s "$Value"; done
New contributor
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f519013%2fassign-the-same-string-to-multiple-variables%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 var1..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 varT,z,3="$value"
It's safer than the printf
approach in the comments because it can handle spaces in $value
.
Please see my EDIT1. Theprintf
method you gave was just fine if you put the declare in front :declare $(printf .....)
– SebMa
May 15 at 7:56
With EDIT1 usingprintf
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 thedeclare
approach is still dangerous if any of the variables were previously declared as arrays. Try aftervar3=() value='([0$(echo Gotcha>&2)]=)'
– Stéphane Chazelas
May 15 at 12:01
eval
is fine if used aseval var1..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
add a comment |
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 var1..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 varT,z,3="$value"
It's safer than the printf
approach in the comments because it can handle spaces in $value
.
Please see my EDIT1. Theprintf
method you gave was just fine if you put the declare in front :declare $(printf .....)
– SebMa
May 15 at 7:56
With EDIT1 usingprintf
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 thedeclare
approach is still dangerous if any of the variables were previously declared as arrays. Try aftervar3=() value='([0$(echo Gotcha>&2)]=)'
– Stéphane Chazelas
May 15 at 12:01
eval
is fine if used aseval var1..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
add a comment |
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 var1..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 varT,z,3="$value"
It's safer than the printf
approach in the comments because it can handle spaces in $value
.
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 var1..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 varT,z,3="$value"
It's safer than the printf
approach in the comments because it can handle spaces in $value
.
edited May 15 at 8:08
answered May 15 at 7:30
laenkeiolaenkeio
4617
4617
Please see my EDIT1. Theprintf
method you gave was just fine if you put the declare in front :declare $(printf .....)
– SebMa
May 15 at 7:56
With EDIT1 usingprintf
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 thedeclare
approach is still dangerous if any of the variables were previously declared as arrays. Try aftervar3=() value='([0$(echo Gotcha>&2)]=)'
– Stéphane Chazelas
May 15 at 12:01
eval
is fine if used aseval var1..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
add a comment |
Please see my EDIT1. Theprintf
method you gave was just fine if you put the declare in front :declare $(printf .....)
– SebMa
May 15 at 7:56
With EDIT1 usingprintf
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 thedeclare
approach is still dangerous if any of the variables were previously declared as arrays. Try aftervar3=() value='([0$(echo Gotcha>&2)]=)'
– Stéphane Chazelas
May 15 at 12:01
eval
is fine if used aseval var1..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
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
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
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
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 var1..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
eval
is fine if used as eval var1..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
add a comment |
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]=%sn' "$var[5]"
or an associative array,
declare -A var
var=( ["T"]=$value ["z"]=$value [3]=$value )
printf 'var[T]=%sn' "$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 runbash
4.3+ to use name references.
– Kusalananda♦
May 15 at 8:09
add a comment |
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]=%sn' "$var[5]"
or an associative array,
declare -A var
var=( ["T"]=$value ["z"]=$value [3]=$value )
printf 'var[T]=%sn' "$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 runbash
4.3+ to use name references.
– Kusalananda♦
May 15 at 8:09
add a comment |
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]=%sn' "$var[5]"
or an associative array,
declare -A var
var=( ["T"]=$value ["z"]=$value [3]=$value )
printf 'var[T]=%sn' "$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.
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]=%sn' "$var[5]"
or an associative array,
declare -A var
var=( ["T"]=$value ["z"]=$value [3]=$value )
printf 'var[T]=%sn' "$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.
edited May 15 at 8:07
answered May 15 at 7:50
Kusalananda♦Kusalananda
147k18278462
147k18278462
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 runbash
4.3+ to use name references.
– Kusalananda♦
May 15 at 8:09
add a comment |
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 runbash
4.3+ to use name references.
– Kusalananda♦
May 15 at 8:09
What does the
-n
option do? I get an error with it. Ah... namerefs (>= 4.3, IIRC?)– muru
May 15 at 8:08
What does the
-n
option do? I get an error with it. Ah... namerefs (>= 4.3, IIRC?)– muru
May 15 at 8:08
1
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
@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
add a comment |
value=balabala
eval var1..10=$value
echo $var1..10
add a comment |
value=balabala
eval var1..10=$value
echo $var1..10
add a comment |
value=balabala
eval var1..10=$value
echo $var1..10
value=balabala
eval var1..10=$value
echo $var1..10
edited May 15 at 12:16
Stéphane Chazelas
319k57603969
319k57603969
answered May 15 at 7:39
dedowsdidedowsdi
2993
2993
add a comment |
add a comment |
Value='-%% this is a test %%-'
for VarName in myVar1 myVar2 myOtherVar; do printf -v "$VarName" %s "$Value"; done
New contributor
add a comment |
Value='-%% this is a test %%-'
for VarName in myVar1 myVar2 myOtherVar; do printf -v "$VarName" %s "$Value"; done
New contributor
add a comment |
Value='-%% this is a test %%-'
for VarName in myVar1 myVar2 myOtherVar; do printf -v "$VarName" %s "$Value"; done
New contributor
Value='-%% this is a test %%-'
for VarName in myVar1 myVar2 myOtherVar; do printf -v "$VarName" %s "$Value"; done
New contributor
edited May 15 at 12:20
Stéphane Chazelas
319k57603969
319k57603969
New contributor
answered May 15 at 11:08
BuygrushBuygrush
211
211
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f519013%2fassign-the-same-string-to-multiple-variables%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Shouldn't the question be: Assign the same string value to multiple variables? (subject and object switched)?
– rexkogitans
May 15 at 9:30