Script runs command fine, but fails when trying to save output to variable2019 Community Moderator ElectionAdd arguments from previous command to zsh completionZSH command runs in shell but not in scriptHow to deal with filenames containing a single quote inside a zsh completion function?Why can't I print a variable I can see in the output of env?Sed command runs with hardcoded value in regex but fails with variable in scriptGetting no output from command substitution?Posix shell script - Save multi line command output to variableScript Runs Fine On 1Machine, Gives Error On OtherScript Runs Fine In Terminal, But Not Under CronNeed a awk/sed to replace the values present inside $
How to deal with or prevent idle in the test team?
Is there enough fresh water in the world to eradicate the drinking water crisis?
Stereotypical names
Can I use my Chinese passport to enter China after I acquired another citizenship?
Organic chemistry Iodoform Reaction
Can I Retrieve Email Addresses from BCC?
Could solar power be utilized and substitute coal in the 19th century?
No idea how to draw this using tikz
Lifted its hind leg on or lifted its hind leg towards?
My boss asked me to take a one-day class, then signs it up as a day off
The One-Electron Universe postulate is true - what simple change can I make to change the whole universe?
Is it okay / does it make sense for another player to join a running game of Munchkin?
Did US corporations pay demonstrators in the German demonstrations against article 13?
Adding empty element to declared container without declaring type of element
How can I raise concerns with a new DM about XP splitting?
How to check participants in at events?
Meta programming: Declare a new struct on the fly
Have I saved too much for retirement so far?
Lightning Web Component - do I need to track changes for every single input field in a form
Why does this part of the Space Shuttle launch pad seem to be floating in air?
Why are all the doors on Ferenginar (the Ferengi home world) far shorter than the average Ferengi?
A workplace installs custom certificates on personal devices, can this be used to decrypt HTTPS traffic?
In Star Trek IV, why did the Bounty go back to a time when whales were already rare?
Invariance of results when scaling explanatory variables in logistic regression, is there a proof?
Script runs command fine, but fails when trying to save output to variable
2019 Community Moderator ElectionAdd arguments from previous command to zsh completionZSH command runs in shell but not in scriptHow to deal with filenames containing a single quote inside a zsh completion function?Why can't I print a variable I can see in the output of env?Sed command runs with hardcoded value in regex but fails with variable in scriptGetting no output from command substitution?Posix shell script - Save multi line command output to variableScript Runs Fine On 1Machine, Gives Error On OtherScript Runs Fine In Terminal, But Not Under CronNeed a awk/sed to replace the values present inside $
I have a shell script with the following contents. I call it with ./script.sh
. Echo successfully runs, but not when I try to save its output to a variable. The same is true for all commands I've tested so far; ls
, pwd
, node
, etc…
#!/bin/zsh
echo foo
# foo
output=$("echo foo")
# command not found: echo foo
How come?
EDIT: Fix: the last echo is inside a string!
shell-script zsh path echo
|
show 2 more comments
I have a shell script with the following contents. I call it with ./script.sh
. Echo successfully runs, but not when I try to save its output to a variable. The same is true for all commands I've tested so far; ls
, pwd
, node
, etc…
#!/bin/zsh
echo foo
# foo
output=$("echo foo")
# command not found: echo foo
How come?
EDIT: Fix: the last echo is inside a string!
shell-script zsh path echo
The only way I'm able to reproduce this is if I quoteecho foo
like:output=$('echo foo')
. Or if I escape the space likeoutput=$(echo foo)
.
– Jesse_b
2 days ago
1
can't reproduce here; do you have something odd in your zsh profile?
– Jeff Schaller
2 days ago
I'm sorry, The last echo is supposed to reside in a string.
– Audun Olsen
2 days ago
1
running"echo foo"
would result in a similar error; what's your goal?
– Jeff Schaller
2 days ago
Simply saving the output to a variable. But I see that it being inside a string may be my mistake…
– Audun Olsen
2 days ago
|
show 2 more comments
I have a shell script with the following contents. I call it with ./script.sh
. Echo successfully runs, but not when I try to save its output to a variable. The same is true for all commands I've tested so far; ls
, pwd
, node
, etc…
#!/bin/zsh
echo foo
# foo
output=$("echo foo")
# command not found: echo foo
How come?
EDIT: Fix: the last echo is inside a string!
shell-script zsh path echo
I have a shell script with the following contents. I call it with ./script.sh
. Echo successfully runs, but not when I try to save its output to a variable. The same is true for all commands I've tested so far; ls
, pwd
, node
, etc…
#!/bin/zsh
echo foo
# foo
output=$("echo foo")
# command not found: echo foo
How come?
EDIT: Fix: the last echo is inside a string!
shell-script zsh path echo
shell-script zsh path echo
edited 2 days ago
Audun Olsen
asked 2 days ago
Audun OlsenAudun Olsen
133
133
The only way I'm able to reproduce this is if I quoteecho foo
like:output=$('echo foo')
. Or if I escape the space likeoutput=$(echo foo)
.
– Jesse_b
2 days ago
1
can't reproduce here; do you have something odd in your zsh profile?
– Jeff Schaller
2 days ago
I'm sorry, The last echo is supposed to reside in a string.
– Audun Olsen
2 days ago
1
running"echo foo"
would result in a similar error; what's your goal?
– Jeff Schaller
2 days ago
Simply saving the output to a variable. But I see that it being inside a string may be my mistake…
– Audun Olsen
2 days ago
|
show 2 more comments
The only way I'm able to reproduce this is if I quoteecho foo
like:output=$('echo foo')
. Or if I escape the space likeoutput=$(echo foo)
.
– Jesse_b
2 days ago
1
can't reproduce here; do you have something odd in your zsh profile?
– Jeff Schaller
2 days ago
I'm sorry, The last echo is supposed to reside in a string.
– Audun Olsen
2 days ago
1
running"echo foo"
would result in a similar error; what's your goal?
– Jeff Schaller
2 days ago
Simply saving the output to a variable. But I see that it being inside a string may be my mistake…
– Audun Olsen
2 days ago
The only way I'm able to reproduce this is if I quote
echo foo
like: output=$('echo foo')
. Or if I escape the space like output=$(echo foo)
.– Jesse_b
2 days ago
The only way I'm able to reproduce this is if I quote
echo foo
like: output=$('echo foo')
. Or if I escape the space like output=$(echo foo)
.– Jesse_b
2 days ago
1
1
can't reproduce here; do you have something odd in your zsh profile?
– Jeff Schaller
2 days ago
can't reproduce here; do you have something odd in your zsh profile?
– Jeff Schaller
2 days ago
I'm sorry, The last echo is supposed to reside in a string.
– Audun Olsen
2 days ago
I'm sorry, The last echo is supposed to reside in a string.
– Audun Olsen
2 days ago
1
1
running
"echo foo"
would result in a similar error; what's your goal?– Jeff Schaller
2 days ago
running
"echo foo"
would result in a similar error; what's your goal?– Jeff Schaller
2 days ago
Simply saving the output to a variable. But I see that it being inside a string may be my mistake…
– Audun Olsen
2 days ago
Simply saving the output to a variable. But I see that it being inside a string may be my mistake…
– Audun Olsen
2 days ago
|
show 2 more comments
2 Answers
2
active
oldest
votes
It seems like you found the issue. You should quote only the string passed to echo
and not the entire command itself:
output=$("echo foo")
Should instead be:
output=$(echo "foo")
If you want the output
variable to contain the literal string echo foo
then you should just set it as:
output='echo foo'
but could also do:
output=$(echo 'echo foo')
Thank you, this works. An additional question; if i replace echo withnode
, which was my initial goal, calling a node-script. I getcommand not found: node
despite node being inside my $PATH variable. Which I checked by echoing path from inside the script. Thoughts?
– Audun Olsen
2 days ago
Without seeing the command you're attempting to execute and your path I can't really speculate.
– Jesse_b
2 days ago
node script.js
succeeds,output=$(node script.js)
fails. Falls perfectly in line with the question title, actually.
– Audun Olsen
2 days ago
add a comment |
As one might expect commands inside parentheses creates a subshell. But $() is not what you might think. It does not return the output of a command as a string it substitutes the output of a command into the shell. If you want proof try this: $(echo "echo 123")
Instead you want to use double quotes to capture it into a string instead of the array (the output split by spaces) being run on the shell.
output="$(echo foo)"
Note this isn't an issue specific to zsh it applies to bash as well.
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
);
);
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%2f508181%2fscript-runs-command-fine-but-fails-when-trying-to-save-output-to-variable%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It seems like you found the issue. You should quote only the string passed to echo
and not the entire command itself:
output=$("echo foo")
Should instead be:
output=$(echo "foo")
If you want the output
variable to contain the literal string echo foo
then you should just set it as:
output='echo foo'
but could also do:
output=$(echo 'echo foo')
Thank you, this works. An additional question; if i replace echo withnode
, which was my initial goal, calling a node-script. I getcommand not found: node
despite node being inside my $PATH variable. Which I checked by echoing path from inside the script. Thoughts?
– Audun Olsen
2 days ago
Without seeing the command you're attempting to execute and your path I can't really speculate.
– Jesse_b
2 days ago
node script.js
succeeds,output=$(node script.js)
fails. Falls perfectly in line with the question title, actually.
– Audun Olsen
2 days ago
add a comment |
It seems like you found the issue. You should quote only the string passed to echo
and not the entire command itself:
output=$("echo foo")
Should instead be:
output=$(echo "foo")
If you want the output
variable to contain the literal string echo foo
then you should just set it as:
output='echo foo'
but could also do:
output=$(echo 'echo foo')
Thank you, this works. An additional question; if i replace echo withnode
, which was my initial goal, calling a node-script. I getcommand not found: node
despite node being inside my $PATH variable. Which I checked by echoing path from inside the script. Thoughts?
– Audun Olsen
2 days ago
Without seeing the command you're attempting to execute and your path I can't really speculate.
– Jesse_b
2 days ago
node script.js
succeeds,output=$(node script.js)
fails. Falls perfectly in line with the question title, actually.
– Audun Olsen
2 days ago
add a comment |
It seems like you found the issue. You should quote only the string passed to echo
and not the entire command itself:
output=$("echo foo")
Should instead be:
output=$(echo "foo")
If you want the output
variable to contain the literal string echo foo
then you should just set it as:
output='echo foo'
but could also do:
output=$(echo 'echo foo')
It seems like you found the issue. You should quote only the string passed to echo
and not the entire command itself:
output=$("echo foo")
Should instead be:
output=$(echo "foo")
If you want the output
variable to contain the literal string echo foo
then you should just set it as:
output='echo foo'
but could also do:
output=$(echo 'echo foo')
answered 2 days ago
Jesse_bJesse_b
14k23572
14k23572
Thank you, this works. An additional question; if i replace echo withnode
, which was my initial goal, calling a node-script. I getcommand not found: node
despite node being inside my $PATH variable. Which I checked by echoing path from inside the script. Thoughts?
– Audun Olsen
2 days ago
Without seeing the command you're attempting to execute and your path I can't really speculate.
– Jesse_b
2 days ago
node script.js
succeeds,output=$(node script.js)
fails. Falls perfectly in line with the question title, actually.
– Audun Olsen
2 days ago
add a comment |
Thank you, this works. An additional question; if i replace echo withnode
, which was my initial goal, calling a node-script. I getcommand not found: node
despite node being inside my $PATH variable. Which I checked by echoing path from inside the script. Thoughts?
– Audun Olsen
2 days ago
Without seeing the command you're attempting to execute and your path I can't really speculate.
– Jesse_b
2 days ago
node script.js
succeeds,output=$(node script.js)
fails. Falls perfectly in line with the question title, actually.
– Audun Olsen
2 days ago
Thank you, this works. An additional question; if i replace echo with
node
, which was my initial goal, calling a node-script. I get command not found: node
despite node being inside my $PATH variable. Which I checked by echoing path from inside the script. Thoughts?– Audun Olsen
2 days ago
Thank you, this works. An additional question; if i replace echo with
node
, which was my initial goal, calling a node-script. I get command not found: node
despite node being inside my $PATH variable. Which I checked by echoing path from inside the script. Thoughts?– Audun Olsen
2 days ago
Without seeing the command you're attempting to execute and your path I can't really speculate.
– Jesse_b
2 days ago
Without seeing the command you're attempting to execute and your path I can't really speculate.
– Jesse_b
2 days ago
node script.js
succeeds, output=$(node script.js)
fails. Falls perfectly in line with the question title, actually.– Audun Olsen
2 days ago
node script.js
succeeds, output=$(node script.js)
fails. Falls perfectly in line with the question title, actually.– Audun Olsen
2 days ago
add a comment |
As one might expect commands inside parentheses creates a subshell. But $() is not what you might think. It does not return the output of a command as a string it substitutes the output of a command into the shell. If you want proof try this: $(echo "echo 123")
Instead you want to use double quotes to capture it into a string instead of the array (the output split by spaces) being run on the shell.
output="$(echo foo)"
Note this isn't an issue specific to zsh it applies to bash as well.
add a comment |
As one might expect commands inside parentheses creates a subshell. But $() is not what you might think. It does not return the output of a command as a string it substitutes the output of a command into the shell. If you want proof try this: $(echo "echo 123")
Instead you want to use double quotes to capture it into a string instead of the array (the output split by spaces) being run on the shell.
output="$(echo foo)"
Note this isn't an issue specific to zsh it applies to bash as well.
add a comment |
As one might expect commands inside parentheses creates a subshell. But $() is not what you might think. It does not return the output of a command as a string it substitutes the output of a command into the shell. If you want proof try this: $(echo "echo 123")
Instead you want to use double quotes to capture it into a string instead of the array (the output split by spaces) being run on the shell.
output="$(echo foo)"
Note this isn't an issue specific to zsh it applies to bash as well.
As one might expect commands inside parentheses creates a subshell. But $() is not what you might think. It does not return the output of a command as a string it substitutes the output of a command into the shell. If you want proof try this: $(echo "echo 123")
Instead you want to use double quotes to capture it into a string instead of the array (the output split by spaces) being run on the shell.
output="$(echo foo)"
Note this isn't an issue specific to zsh it applies to bash as well.
answered 2 days ago
jdwolfjdwolf
2,833217
2,833217
add a comment |
add a comment |
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%2f508181%2fscript-runs-command-fine-but-fails-when-trying-to-save-output-to-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
-echo, path, shell-script, zsh
The only way I'm able to reproduce this is if I quote
echo foo
like:output=$('echo foo')
. Or if I escape the space likeoutput=$(echo foo)
.– Jesse_b
2 days ago
1
can't reproduce here; do you have something odd in your zsh profile?
– Jeff Schaller
2 days ago
I'm sorry, The last echo is supposed to reside in a string.
– Audun Olsen
2 days ago
1
running
"echo foo"
would result in a similar error; what's your goal?– Jeff Schaller
2 days ago
Simply saving the output to a variable. But I see that it being inside a string may be my mistake…
– Audun Olsen
2 days ago