Unable to assign env variable using a command's output
up vote
1
down vote
favorite
I can set environment variables like this.
➤ foo=bar env | grep foo
foo=bar
But, what if, I can only get foo=bar
string after I execute a command (which is my use-case). The simplest way to emulate this is using a simple echo
command.
➤ `echo foo=bar` env | grep foo
zsh: command not found: foo=bar
In this, zsh/bash starts interpreting it as a command. How do I fix this?
bash zsh
add a comment |
up vote
1
down vote
favorite
I can set environment variables like this.
➤ foo=bar env | grep foo
foo=bar
But, what if, I can only get foo=bar
string after I execute a command (which is my use-case). The simplest way to emulate this is using a simple echo
command.
➤ `echo foo=bar` env | grep foo
zsh: command not found: foo=bar
In this, zsh/bash starts interpreting it as a command. How do I fix this?
bash zsh
1
It's not clear what you are asking.
– Emily E.
Nov 22 at 13:24
1
FWIW, I find this question very clear. I think it's a good example of How to create a Minimal, Complete, and Verifiable example (MCVE).
– pjh
Nov 22 at 19:18
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I can set environment variables like this.
➤ foo=bar env | grep foo
foo=bar
But, what if, I can only get foo=bar
string after I execute a command (which is my use-case). The simplest way to emulate this is using a simple echo
command.
➤ `echo foo=bar` env | grep foo
zsh: command not found: foo=bar
In this, zsh/bash starts interpreting it as a command. How do I fix this?
bash zsh
I can set environment variables like this.
➤ foo=bar env | grep foo
foo=bar
But, what if, I can only get foo=bar
string after I execute a command (which is my use-case). The simplest way to emulate this is using a simple echo
command.
➤ `echo foo=bar` env | grep foo
zsh: command not found: foo=bar
In this, zsh/bash starts interpreting it as a command. How do I fix this?
bash zsh
bash zsh
asked Nov 22 at 12:50
Abhijeet Rastogi
9,9191964108
9,9191964108
1
It's not clear what you are asking.
– Emily E.
Nov 22 at 13:24
1
FWIW, I find this question very clear. I think it's a good example of How to create a Minimal, Complete, and Verifiable example (MCVE).
– pjh
Nov 22 at 19:18
add a comment |
1
It's not clear what you are asking.
– Emily E.
Nov 22 at 13:24
1
FWIW, I find this question very clear. I think it's a good example of How to create a Minimal, Complete, and Verifiable example (MCVE).
– pjh
Nov 22 at 19:18
1
1
It's not clear what you are asking.
– Emily E.
Nov 22 at 13:24
It's not clear what you are asking.
– Emily E.
Nov 22 at 13:24
1
1
FWIW, I find this question very clear. I think it's a good example of How to create a Minimal, Complete, and Verifiable example (MCVE).
– pjh
Nov 22 at 19:18
FWIW, I find this question very clear. I think it's a good example of How to create a Minimal, Complete, and Verifiable example (MCVE).
– pjh
Nov 22 at 19:18
add a comment |
3 Answers
3
active
oldest
votes
up vote
3
down vote
The problem is that the shell looks for the form var1=val1 var2=val2 ... command
before doing expansions, including command substitution (`...`
or $(...)
).
One way to work around the problem is to use eval
to cause the shell to do its parsing after the command substitution is done:
eval "$(echo foo=bar)" env | grep foo
(See What is the benefit of using $() instead of backticks in shell scripts? for an explanation of why I've changed `...`
to $(...)
.)
Unfortunately, eval
is potentially dangerous. It should not be used unless there is no other alternative. See Why should eval be avoided in Bash, and what should I use instead?.
Another alternative is to use export
in a subshell. One way is:
{ export "$(echo foo=bar)" ; env ; } | grep foo
Since the export
is done in a pipeline (and not the last part of a pipeline, which would make a difference for some shells or some modes) it doesn't affect the variable settings in the current shell. If the command is not in a pipeline, then it would be necessary to explicitly run it in a subshell to get the same effect. E.g.
( export "$(echo foo=bar)" ; env ) > tmpfile
grep foo tmpfile
add a comment |
up vote
0
down vote
It is working for me as expected. Have you tried like below?
env `echo foo=bar` | grep foo
add a comment |
up vote
0
down vote
I'm not sure if I understood your question, but try this:
$ echo "hello" && foo=bar env | grep foo
hello
foo=bar
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53431446%2funable-to-assign-env-variable-using-a-commands-output%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
up vote
3
down vote
The problem is that the shell looks for the form var1=val1 var2=val2 ... command
before doing expansions, including command substitution (`...`
or $(...)
).
One way to work around the problem is to use eval
to cause the shell to do its parsing after the command substitution is done:
eval "$(echo foo=bar)" env | grep foo
(See What is the benefit of using $() instead of backticks in shell scripts? for an explanation of why I've changed `...`
to $(...)
.)
Unfortunately, eval
is potentially dangerous. It should not be used unless there is no other alternative. See Why should eval be avoided in Bash, and what should I use instead?.
Another alternative is to use export
in a subshell. One way is:
{ export "$(echo foo=bar)" ; env ; } | grep foo
Since the export
is done in a pipeline (and not the last part of a pipeline, which would make a difference for some shells or some modes) it doesn't affect the variable settings in the current shell. If the command is not in a pipeline, then it would be necessary to explicitly run it in a subshell to get the same effect. E.g.
( export "$(echo foo=bar)" ; env ) > tmpfile
grep foo tmpfile
add a comment |
up vote
3
down vote
The problem is that the shell looks for the form var1=val1 var2=val2 ... command
before doing expansions, including command substitution (`...`
or $(...)
).
One way to work around the problem is to use eval
to cause the shell to do its parsing after the command substitution is done:
eval "$(echo foo=bar)" env | grep foo
(See What is the benefit of using $() instead of backticks in shell scripts? for an explanation of why I've changed `...`
to $(...)
.)
Unfortunately, eval
is potentially dangerous. It should not be used unless there is no other alternative. See Why should eval be avoided in Bash, and what should I use instead?.
Another alternative is to use export
in a subshell. One way is:
{ export "$(echo foo=bar)" ; env ; } | grep foo
Since the export
is done in a pipeline (and not the last part of a pipeline, which would make a difference for some shells or some modes) it doesn't affect the variable settings in the current shell. If the command is not in a pipeline, then it would be necessary to explicitly run it in a subshell to get the same effect. E.g.
( export "$(echo foo=bar)" ; env ) > tmpfile
grep foo tmpfile
add a comment |
up vote
3
down vote
up vote
3
down vote
The problem is that the shell looks for the form var1=val1 var2=val2 ... command
before doing expansions, including command substitution (`...`
or $(...)
).
One way to work around the problem is to use eval
to cause the shell to do its parsing after the command substitution is done:
eval "$(echo foo=bar)" env | grep foo
(See What is the benefit of using $() instead of backticks in shell scripts? for an explanation of why I've changed `...`
to $(...)
.)
Unfortunately, eval
is potentially dangerous. It should not be used unless there is no other alternative. See Why should eval be avoided in Bash, and what should I use instead?.
Another alternative is to use export
in a subshell. One way is:
{ export "$(echo foo=bar)" ; env ; } | grep foo
Since the export
is done in a pipeline (and not the last part of a pipeline, which would make a difference for some shells or some modes) it doesn't affect the variable settings in the current shell. If the command is not in a pipeline, then it would be necessary to explicitly run it in a subshell to get the same effect. E.g.
( export "$(echo foo=bar)" ; env ) > tmpfile
grep foo tmpfile
The problem is that the shell looks for the form var1=val1 var2=val2 ... command
before doing expansions, including command substitution (`...`
or $(...)
).
One way to work around the problem is to use eval
to cause the shell to do its parsing after the command substitution is done:
eval "$(echo foo=bar)" env | grep foo
(See What is the benefit of using $() instead of backticks in shell scripts? for an explanation of why I've changed `...`
to $(...)
.)
Unfortunately, eval
is potentially dangerous. It should not be used unless there is no other alternative. See Why should eval be avoided in Bash, and what should I use instead?.
Another alternative is to use export
in a subshell. One way is:
{ export "$(echo foo=bar)" ; env ; } | grep foo
Since the export
is done in a pipeline (and not the last part of a pipeline, which would make a difference for some shells or some modes) it doesn't affect the variable settings in the current shell. If the command is not in a pipeline, then it would be necessary to explicitly run it in a subshell to get the same effect. E.g.
( export "$(echo foo=bar)" ; env ) > tmpfile
grep foo tmpfile
answered Nov 22 at 19:43
pjh
1,584611
1,584611
add a comment |
add a comment |
up vote
0
down vote
It is working for me as expected. Have you tried like below?
env `echo foo=bar` | grep foo
add a comment |
up vote
0
down vote
It is working for me as expected. Have you tried like below?
env `echo foo=bar` | grep foo
add a comment |
up vote
0
down vote
up vote
0
down vote
It is working for me as expected. Have you tried like below?
env `echo foo=bar` | grep foo
It is working for me as expected. Have you tried like below?
env `echo foo=bar` | grep foo
answered Nov 22 at 13:02
Mohit Kumar
584420
584420
add a comment |
add a comment |
up vote
0
down vote
I'm not sure if I understood your question, but try this:
$ echo "hello" && foo=bar env | grep foo
hello
foo=bar
add a comment |
up vote
0
down vote
I'm not sure if I understood your question, but try this:
$ echo "hello" && foo=bar env | grep foo
hello
foo=bar
add a comment |
up vote
0
down vote
up vote
0
down vote
I'm not sure if I understood your question, but try this:
$ echo "hello" && foo=bar env | grep foo
hello
foo=bar
I'm not sure if I understood your question, but try this:
$ echo "hello" && foo=bar env | grep foo
hello
foo=bar
answered Nov 22 at 13:03
downtheroad
535
535
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2fstackoverflow.com%2fquestions%2f53431446%2funable-to-assign-env-variable-using-a-commands-output%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
It's not clear what you are asking.
– Emily E.
Nov 22 at 13:24
1
FWIW, I find this question very clear. I think it's a good example of How to create a Minimal, Complete, and Verifiable example (MCVE).
– pjh
Nov 22 at 19:18