Indirectly access environment variableNeed to set a variable with “[]”Serialize shell variable in bash or...
Are there any modern advantages of a fire piston?
Explain the objections to these measures against human trafficking
What is the lore-based reason that the Spectator has the Create Food and Water trait, instead of simply not requiring food and water?
Roman Numerals equation 1
Why are the books in the Game of Thrones citadel library shelved spine inwards?
How to prevent cleaner from hanging my lock screen in Ubuntu 16.04
Why did other German political parties disband so fast when Hitler was appointed chancellor?
Porting Linux to another platform requirements
What is the purpose of easy combat scenarios that don't need resource expenditure?
Can an insurance company drop you after receiving a bill and refusing to pay?
Propeller Fan - Array Modifier
Why exactly do action photographers need high fps burst cameras?
Cookies - Should the toggles be on?
How much mayhem could I cause as a sentient fish?
Highly technological aliens land nuclear fusion powered ships in medieval city and slaughter everyone, using swords?
Would a National Army of mercenaries be a feasible idea?
Who is this Ant Woman character in this image alongside the Wasp?
Error in a formula field
Why did the villain in the first Men in Black movie care about Earth's Cockroaches?
Can making a creature unable to attack after it has been assigned as an attacker remove it from combat?
Why would space fleets be aligned?
How can animals be objects of ethics without being subjects as well?
Why Normality assumption in linear regression
Why publish a research paper when a blog post or a lecture slide can have more citation count than a journal paper?
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 31 mins ago
Kusalananda
133k17253416
133k17253416
New contributor
asked 39 mins 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
25 mins 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
25 mins 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
25 mins 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 24 mins ago
answered 35 mins ago
KusalanandaKusalananda
133k17253416
133k17253416
Perfect thanks ... env_val="${!ev}"
– PaulB
25 mins ago
add a comment |
Perfect thanks ... env_val="${!ev}"
– PaulB
25 mins ago
Perfect thanks ... env_val="${!ev}"
– PaulB
25 mins ago
Perfect thanks ... env_val="${!ev}"
– PaulB
25 mins 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 16 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 15 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