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 $










0















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!










share|improve this question
























  • 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





    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















0















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!










share|improve this question
























  • 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





    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













0












0








0








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!










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 quote echo foo like: output=$('echo foo'). Or if I escape the space like output=$(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







  • 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










2 Answers
2






active

oldest

votes


















1














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')





share|improve this answer























  • 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











  • node script.js succeeds, output=$(node script.js) fails. Falls perfectly in line with the question title, actually.

    – Audun Olsen
    2 days ago


















0














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.






share|improve this answer






















    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
    );



    );













    draft saved

    draft discarded


















    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









    1














    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')





    share|improve this answer























    • 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











    • node script.js succeeds, output=$(node script.js) fails. Falls perfectly in line with the question title, actually.

      – Audun Olsen
      2 days ago















    1














    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')





    share|improve this answer























    • 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











    • node script.js succeeds, output=$(node script.js) fails. Falls perfectly in line with the question title, actually.

      – Audun Olsen
      2 days ago













    1












    1








    1







    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')





    share|improve this answer













    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')






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 2 days ago









    Jesse_bJesse_b

    14k23572




    14k23572












    • 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











    • 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












    • 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













    0














    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.






    share|improve this answer



























      0














      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.






      share|improve this answer

























        0












        0








        0







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 days ago









        jdwolfjdwolf

        2,833217




        2,833217



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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

            Popular posts from this blog

            Frič See also Navigation menuinternal link

            Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

            fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox