Reference bash variable from a variable [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
This question already has an answer here:
How to do indirect variable evaluation
5 answers
How can I reference a variable in bash based on another variable? Let me setup the example:
package="foobar"
# the variable I wish to reference is $foobar_darwin_amd64
# thus trying:
echo "$package_darwin_amd64"
But this does not work.
bash variable-substitution
marked as duplicate by Barmar, Jeff Schaller, G-Man, Kusalananda
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Feb 1 at 6:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
0
down vote
favorite
This question already has an answer here:
How to do indirect variable evaluation
5 answers
How can I reference a variable in bash based on another variable? Let me setup the example:
package="foobar"
# the variable I wish to reference is $foobar_darwin_amd64
# thus trying:
echo "$package_darwin_amd64"
But this does not work.
bash variable-substitution
marked as duplicate by Barmar, Jeff Schaller, G-Man, Kusalananda
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Feb 1 at 6:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
How to do indirect variable evaluation
5 answers
How can I reference a variable in bash based on another variable? Let me setup the example:
package="foobar"
# the variable I wish to reference is $foobar_darwin_amd64
# thus trying:
echo "$package_darwin_amd64"
But this does not work.
bash variable-substitution
This question already has an answer here:
How to do indirect variable evaluation
5 answers
How can I reference a variable in bash based on another variable? Let me setup the example:
package="foobar"
# the variable I wish to reference is $foobar_darwin_amd64
# thus trying:
echo "$package_darwin_amd64"
But this does not work.
This question already has an answer here:
How to do indirect variable evaluation
5 answers
bash variable-substitution
asked Jan 31 at 23:27
Justin
1112
1112
marked as duplicate by Barmar, Jeff Schaller, G-Man, Kusalananda
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Feb 1 at 6:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Barmar, Jeff Schaller, G-Man, Kusalananda
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Feb 1 at 6:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
If you shell supports the $!varname
form of indirect references, you can do (as suggested by @Barmar):
$ foobar_darwin_amd64=pinto
$ package=foobar
$ varname="$package_darwin_amd64"
$ echo $!varname
pinto
Otherwise, you can use eval
:
$ foobar_darwin_amd64=pinto
$ package=foobar
$ eval echo $$package_darwin_amd64
pinto
That said, using eval
has some risks associated with it, see this link for more discussion.
Why useeval
when you can use an indirect reference?
â Barmar
Jan 31 at 23:33
add a comment |Â
up vote
1
down vote
Here is the solution that worked for me:
VARNAME="$package_darwin_amd64"
echo "$!VARNAME"
add a comment |Â
up vote
1
down vote
In bash v4 you can use a "nameref"
$ foobar_darwin_amd64=pinto
$ package=foobar
$ declare -n var=$package_darwin_amd64
$ echo $var
pinto
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
If you shell supports the $!varname
form of indirect references, you can do (as suggested by @Barmar):
$ foobar_darwin_amd64=pinto
$ package=foobar
$ varname="$package_darwin_amd64"
$ echo $!varname
pinto
Otherwise, you can use eval
:
$ foobar_darwin_amd64=pinto
$ package=foobar
$ eval echo $$package_darwin_amd64
pinto
That said, using eval
has some risks associated with it, see this link for more discussion.
Why useeval
when you can use an indirect reference?
â Barmar
Jan 31 at 23:33
add a comment |Â
up vote
2
down vote
If you shell supports the $!varname
form of indirect references, you can do (as suggested by @Barmar):
$ foobar_darwin_amd64=pinto
$ package=foobar
$ varname="$package_darwin_amd64"
$ echo $!varname
pinto
Otherwise, you can use eval
:
$ foobar_darwin_amd64=pinto
$ package=foobar
$ eval echo $$package_darwin_amd64
pinto
That said, using eval
has some risks associated with it, see this link for more discussion.
Why useeval
when you can use an indirect reference?
â Barmar
Jan 31 at 23:33
add a comment |Â
up vote
2
down vote
up vote
2
down vote
If you shell supports the $!varname
form of indirect references, you can do (as suggested by @Barmar):
$ foobar_darwin_amd64=pinto
$ package=foobar
$ varname="$package_darwin_amd64"
$ echo $!varname
pinto
Otherwise, you can use eval
:
$ foobar_darwin_amd64=pinto
$ package=foobar
$ eval echo $$package_darwin_amd64
pinto
That said, using eval
has some risks associated with it, see this link for more discussion.
If you shell supports the $!varname
form of indirect references, you can do (as suggested by @Barmar):
$ foobar_darwin_amd64=pinto
$ package=foobar
$ varname="$package_darwin_amd64"
$ echo $!varname
pinto
Otherwise, you can use eval
:
$ foobar_darwin_amd64=pinto
$ package=foobar
$ eval echo $$package_darwin_amd64
pinto
That said, using eval
has some risks associated with it, see this link for more discussion.
edited Jan 31 at 23:46
answered Jan 31 at 23:31
Andy Dalton
4,7561520
4,7561520
Why useeval
when you can use an indirect reference?
â Barmar
Jan 31 at 23:33
add a comment |Â
Why useeval
when you can use an indirect reference?
â Barmar
Jan 31 at 23:33
Why use
eval
when you can use an indirect reference?â Barmar
Jan 31 at 23:33
Why use
eval
when you can use an indirect reference?â Barmar
Jan 31 at 23:33
add a comment |Â
up vote
1
down vote
Here is the solution that worked for me:
VARNAME="$package_darwin_amd64"
echo "$!VARNAME"
add a comment |Â
up vote
1
down vote
Here is the solution that worked for me:
VARNAME="$package_darwin_amd64"
echo "$!VARNAME"
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Here is the solution that worked for me:
VARNAME="$package_darwin_amd64"
echo "$!VARNAME"
Here is the solution that worked for me:
VARNAME="$package_darwin_amd64"
echo "$!VARNAME"
answered Jan 31 at 23:49
Justin
1112
1112
add a comment |Â
add a comment |Â
up vote
1
down vote
In bash v4 you can use a "nameref"
$ foobar_darwin_amd64=pinto
$ package=foobar
$ declare -n var=$package_darwin_amd64
$ echo $var
pinto
add a comment |Â
up vote
1
down vote
In bash v4 you can use a "nameref"
$ foobar_darwin_amd64=pinto
$ package=foobar
$ declare -n var=$package_darwin_amd64
$ echo $var
pinto
add a comment |Â
up vote
1
down vote
up vote
1
down vote
In bash v4 you can use a "nameref"
$ foobar_darwin_amd64=pinto
$ package=foobar
$ declare -n var=$package_darwin_amd64
$ echo $var
pinto
In bash v4 you can use a "nameref"
$ foobar_darwin_amd64=pinto
$ package=foobar
$ declare -n var=$package_darwin_amd64
$ echo $var
pinto
answered Feb 1 at 0:32
glenn jackman
46.5k265103
46.5k265103
add a comment |Â
add a comment |Â