Indirectly access environment variable [duplicate]
This question already has an answer here:
Variable substitution with an exclamation mark in bash
1 answer
Given I have in a bash script
ev=USER
How can I get the environment variable value for $USER using ev?
Tried naively doing:
echo ${"$"$ev}
which results in bad substitution.
I'd expect to get back whatever the value of $USER is.
bash variable
marked as duplicate by terdon♦
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();
}
);
});
});
Mar 1 at 18:22
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 |
This question already has an answer here:
Variable substitution with an exclamation mark in bash
1 answer
Given I have in a bash script
ev=USER
How can I get the environment variable value for $USER using ev?
Tried naively doing:
echo ${"$"$ev}
which results in bad substitution.
I'd expect to get back whatever the value of $USER is.
bash variable
marked as duplicate by terdon♦
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();
}
);
});
});
Mar 1 at 18:22
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 |
This question already has an answer here:
Variable substitution with an exclamation mark in bash
1 answer
Given I have in a bash script
ev=USER
How can I get the environment variable value for $USER using ev?
Tried naively doing:
echo ${"$"$ev}
which results in bad substitution.
I'd expect to get back whatever the value of $USER is.
bash variable
This question already has an answer here:
Variable substitution with an exclamation mark in bash
1 answer
Given I have in a bash script
ev=USER
How can I get the environment variable value for $USER using ev?
Tried naively doing:
echo ${"$"$ev}
which results in bad substitution.
I'd expect to get back whatever the value of $USER is.
This question already has an answer here:
Variable substitution with an exclamation mark in bash
1 answer
bash variable
bash variable
edited Mar 1 at 17:28
Rui F Ribeiro
41.9k1483142
41.9k1483142
asked Mar 1 at 10:21
PaulBPaulB
1234
1234
marked as duplicate by terdon♦
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();
}
);
});
});
Mar 1 at 18:22
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 terdon♦
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();
}
);
});
});
Mar 1 at 18:22
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
By using an indirect expansion (also sometimes called "variable indirection"),
ev=USER
printf '%sn' "${!ev}"
This is described in the bash
(5.0) manual, in the section titled "Parameter Expansion".
Or, by making ev
a name reference (requires bash
4.3+),
declare -n ev=USER
printf '%sn' "$ev"
This is described in the bash
(5.0) manual, just before the section called "Positional Parameters".
Perfect thanks ... env_val="${!ev}"
– PaulB
Mar 1 at 10:36
add a comment |
If it's only about environment variables, as opposed to shell variables, then on most systems, you can use:
printenv -- "$ev"
For shell variables, with any Bourne-like shell, you can do:
eval 'printf "%sn" "${'"$ev"}'}"'
Or with zsh
:
printf '%sn' "${(P)ev}"
Or with bash
:
printf '%sn' "${!ev}"
All 3 are arbitrary command injection vulnerabilities if the content of $ev
is not under your control.
add a comment |
You can also evaluate the command after the vale for $ev
has been substituted:
eval echo "$"$ev
The part "$"$ev
resolves to $USER
so eval
executes echo $USER
.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
By using an indirect expansion (also sometimes called "variable indirection"),
ev=USER
printf '%sn' "${!ev}"
This is described in the bash
(5.0) manual, in the section titled "Parameter Expansion".
Or, by making ev
a name reference (requires bash
4.3+),
declare -n ev=USER
printf '%sn' "$ev"
This is described in the bash
(5.0) manual, just before the section called "Positional Parameters".
Perfect thanks ... env_val="${!ev}"
– PaulB
Mar 1 at 10:36
add a comment |
By using an indirect expansion (also sometimes called "variable indirection"),
ev=USER
printf '%sn' "${!ev}"
This is described in the bash
(5.0) manual, in the section titled "Parameter Expansion".
Or, by making ev
a name reference (requires bash
4.3+),
declare -n ev=USER
printf '%sn' "$ev"
This is described in the bash
(5.0) manual, just before the section called "Positional Parameters".
Perfect thanks ... env_val="${!ev}"
– PaulB
Mar 1 at 10:36
add a comment |
By using an indirect expansion (also sometimes called "variable indirection"),
ev=USER
printf '%sn' "${!ev}"
This is described in the bash
(5.0) manual, in the section titled "Parameter Expansion".
Or, by making ev
a name reference (requires bash
4.3+),
declare -n ev=USER
printf '%sn' "$ev"
This is described in the bash
(5.0) manual, just before the section called "Positional Parameters".
By using an indirect expansion (also sometimes called "variable indirection"),
ev=USER
printf '%sn' "${!ev}"
This is described in the bash
(5.0) manual, in the section titled "Parameter Expansion".
Or, by making ev
a name reference (requires bash
4.3+),
declare -n ev=USER
printf '%sn' "$ev"
This is described in the bash
(5.0) manual, just before the section called "Positional Parameters".
edited Mar 1 at 10:37
answered Mar 1 at 10:26
Kusalananda♦Kusalananda
139k17260432
139k17260432
Perfect thanks ... env_val="${!ev}"
– PaulB
Mar 1 at 10:36
add a comment |
Perfect thanks ... env_val="${!ev}"
– PaulB
Mar 1 at 10:36
Perfect thanks ... env_val="${!ev}"
– PaulB
Mar 1 at 10:36
Perfect thanks ... env_val="${!ev}"
– PaulB
Mar 1 at 10:36
add a comment |
If it's only about environment variables, as opposed to shell variables, then on most systems, you can use:
printenv -- "$ev"
For shell variables, with any Bourne-like shell, you can do:
eval 'printf "%sn" "${'"$ev"}'}"'
Or with zsh
:
printf '%sn' "${(P)ev}"
Or with bash
:
printf '%sn' "${!ev}"
All 3 are arbitrary command injection vulnerabilities if the content of $ev
is not under your control.
add a comment |
If it's only about environment variables, as opposed to shell variables, then on most systems, you can use:
printenv -- "$ev"
For shell variables, with any Bourne-like shell, you can do:
eval 'printf "%sn" "${'"$ev"}'}"'
Or with zsh
:
printf '%sn' "${(P)ev}"
Or with bash
:
printf '%sn' "${!ev}"
All 3 are arbitrary command injection vulnerabilities if the content of $ev
is not under your control.
add a comment |
If it's only about environment variables, as opposed to shell variables, then on most systems, you can use:
printenv -- "$ev"
For shell variables, with any Bourne-like shell, you can do:
eval 'printf "%sn" "${'"$ev"}'}"'
Or with zsh
:
printf '%sn' "${(P)ev}"
Or with bash
:
printf '%sn' "${!ev}"
All 3 are arbitrary command injection vulnerabilities if the content of $ev
is not under your control.
If it's only about environment variables, as opposed to shell variables, then on most systems, you can use:
printenv -- "$ev"
For shell variables, with any Bourne-like shell, you can do:
eval 'printf "%sn" "${'"$ev"}'}"'
Or with zsh
:
printf '%sn' "${(P)ev}"
Or with bash
:
printf '%sn' "${!ev}"
All 3 are arbitrary command injection vulnerabilities if the content of $ev
is not under your control.
answered Mar 1 at 10:45
Stéphane ChazelasStéphane Chazelas
313k57592948
313k57592948
add a comment |
add a comment |
You can also evaluate the command after the vale for $ev
has been substituted:
eval echo "$"$ev
The part "$"$ev
resolves to $USER
so eval
executes echo $USER
.
add a comment |
You can also evaluate the command after the vale for $ev
has been substituted:
eval echo "$"$ev
The part "$"$ev
resolves to $USER
so eval
executes echo $USER
.
add a comment |
You can also evaluate the command after the vale for $ev
has been substituted:
eval echo "$"$ev
The part "$"$ev
resolves to $USER
so eval
executes echo $USER
.
You can also evaluate the command after the vale for $ev
has been substituted:
eval echo "$"$ev
The part "$"$ev
resolves to $USER
so eval
executes echo $USER
.
answered Mar 1 at 10:46
katoshkatosh
1619
1619
add a comment |
add a comment |