Indirectly access environment variableNeed to set a variable with “[]”Serialize shell variable in bash or...
How would an AI self awareness kill switch work?
What is the most triangles you can make from a capital "H" and 3 straight lines?
How do I say "Brexit" in Latin?
Who is this Ant Woman character in this image alongside the Wasp?
Why do stocks necessarily drop during a recession?
How to avoid being sexist when trying to employ someone to function in a very sexist environment?
Is it a fallacy if someone claims they need an explanation for every word of your argument to the point where they don't understand common terms?
Pronunciation of umlaut vowels in the history of German
Can we use the stored gravitational potential energy of a building to produce power?
How to deal with an incendiary email that was recalled
Writing a character who is going through a civilizing process without overdoing it?
Why Normality assumption in linear regression
Differentiate between Local and Global Unitaries
Indirectly access environment variable
How much mayhem could I cause as a sentient fish?
Lick explanation
Traveling through the asteriod belt?
Can a person refuse a presidential pardon?
What kind of hardware implements Fourier transform?
How can animals be objects of ethics without being subjects as well?
How to remove lines through the legend markers in ListPlot?
Why are the books in the Game of Thrones citadel library shelved spine inwards?
Strange Sign on Lab Door
How can my powered armor quickly replace its ceramic plates?
Indirectly access environment variable
Need to set a variable with “[]”Serialize shell variable in bash or zshshell script unable to set environment variable with the grepped valueGet the latest value of an environment variable is a bash shell scriptcopy array with array name inside string in bashEvaluate command stored in Environment variableBash - assign array into variable as stringVariable in while loop only check on initial execution?Environment variable not visible after `sudo su`Process substitution inside a subshell to set a variable
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.
thanks
bash variable
New contributor
add a comment |
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.
thanks
bash variable
New contributor
add a comment |
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.
thanks
bash variable
New contributor
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.
thanks
bash variable
bash variable
New contributor
New contributor
edited 1 hour ago
Kusalananda
133k17253416
133k17253416
New contributor
asked 1 hour ago
PaulBPaulB
1134
1134
New contributor
New contributor
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
1 hour ago
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 |
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
});
}
});
PaulB is a new contributor. Be nice, and check out our Code of Conduct.
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%2f503732%2findirectly-access-environment-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
1 hour ago
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
1 hour ago
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 1 hour ago
answered 1 hour ago
KusalanandaKusalananda
133k17253416
133k17253416
Perfect thanks ... env_val="${!ev}"
– PaulB
1 hour ago
add a comment |
Perfect thanks ... env_val="${!ev}"
– PaulB
1 hour ago
Perfect thanks ... env_val="${!ev}"
– PaulB
1 hour ago
Perfect thanks ... env_val="${!ev}"
– PaulB
1 hour ago
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 57 mins ago
Stéphane ChazelasStéphane Chazelas
308k57581939
308k57581939
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 56 mins ago
katoshkatosh
425
425
add a comment |
add a comment |
PaulB is a new contributor. Be nice, and check out our Code of Conduct.
PaulB is a new contributor. Be nice, and check out our Code of Conduct.
PaulB is a new contributor. Be nice, and check out our Code of Conduct.
PaulB is a new contributor. Be nice, and check out our Code of Conduct.
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%2f503732%2findirectly-access-environment-variable%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