Process substitution inside a subshell to set a variableBad substitution inside xargs subshell?Creating temp...

How to regain lost focus?

Is it possible to narrate a novel in a faux-historical style without alienating the reader?

Does Plato's "Ring of Gyges" have a corrupting influence on its wearer?

How do I avoid the "chosen hero" feeling?

Is Screenshot Time-tracking Common?

If I tried and failed to start my own business, how do I apply for a job without job experience?

Is it really OK to use "because of"?

In a post apocalypse world, with no power and few survivors, would Satnav still work?

How does a mid-19 century Military combat a modernized one?

Alternate timeline nomenclature

Why write a book when there's a movie in my head?

Why do single electrical receptacles exist?

How much is too much when it comes to diagrams in a research article?

Can you say "leftside right"?

filecontents: select rows of group to display

Buying a "Used" Router

Renting a 2CV in France

What does an unprocessed RAW file look like?

Why can't I set the 'prototype' of a function created using 'bind'?

Is it possible to have the same planeswalker from different editions in a Commander deck?

How to deal with an underperforming subordinate?

Protagonist constantly has to have long words explained to her. Will this get tedious?

Is the UK legally prevented from having another referendum on Brexit?

How to wrap a figure in exam document?



Process substitution inside a subshell to set a variable


Bad substitution inside xargs subshell?Creating temp file vs process substitution vs variable expansion?Assign Subshell background process pid to variableScope of variables in a process substitutionbash array with variable in the nameIs the command in a process substitution invoked in a subshell?Subshell and process substitutionSubshell inside quotesSet environment variable for subshellSyntax error near unexpected token 'else'













1















I'm trying to run a script remotely and use its standard output to populate a variable. I'm doing this to avoid temporary files.



Here's the pattern I'm trying:



var=$(bash <(curl -fsSkL http://remote/file.sh))
echo "var=${var}"


I'm testing this pattern without curl using cat:



var=$(bash <(cat ./local/file.sh))
echo "var=${var}"


This should be the same as far as syntax is concerned. ./local/file.sh contains echo hello, so I would expect var to contain the value hello, but alas, executing the above results in the following:



test.sh: command substitution: line 4: syntax error near unexpected token `('
test.sh: command substitution: line 4: `bash <(cat ./local/file.sh)'
var=


How can I accomplish my goal without using temporary files?










share|improve this question



























    1















    I'm trying to run a script remotely and use its standard output to populate a variable. I'm doing this to avoid temporary files.



    Here's the pattern I'm trying:



    var=$(bash <(curl -fsSkL http://remote/file.sh))
    echo "var=${var}"


    I'm testing this pattern without curl using cat:



    var=$(bash <(cat ./local/file.sh))
    echo "var=${var}"


    This should be the same as far as syntax is concerned. ./local/file.sh contains echo hello, so I would expect var to contain the value hello, but alas, executing the above results in the following:



    test.sh: command substitution: line 4: syntax error near unexpected token `('
    test.sh: command substitution: line 4: `bash <(cat ./local/file.sh)'
    var=


    How can I accomplish my goal without using temporary files?










    share|improve this question

























      1












      1








      1








      I'm trying to run a script remotely and use its standard output to populate a variable. I'm doing this to avoid temporary files.



      Here's the pattern I'm trying:



      var=$(bash <(curl -fsSkL http://remote/file.sh))
      echo "var=${var}"


      I'm testing this pattern without curl using cat:



      var=$(bash <(cat ./local/file.sh))
      echo "var=${var}"


      This should be the same as far as syntax is concerned. ./local/file.sh contains echo hello, so I would expect var to contain the value hello, but alas, executing the above results in the following:



      test.sh: command substitution: line 4: syntax error near unexpected token `('
      test.sh: command substitution: line 4: `bash <(cat ./local/file.sh)'
      var=


      How can I accomplish my goal without using temporary files?










      share|improve this question














      I'm trying to run a script remotely and use its standard output to populate a variable. I'm doing this to avoid temporary files.



      Here's the pattern I'm trying:



      var=$(bash <(curl -fsSkL http://remote/file.sh))
      echo "var=${var}"


      I'm testing this pattern without curl using cat:



      var=$(bash <(cat ./local/file.sh))
      echo "var=${var}"


      This should be the same as far as syntax is concerned. ./local/file.sh contains echo hello, so I would expect var to contain the value hello, but alas, executing the above results in the following:



      test.sh: command substitution: line 4: syntax error near unexpected token `('
      test.sh: command substitution: line 4: `bash <(cat ./local/file.sh)'
      var=


      How can I accomplish my goal without using temporary files?







      bash variable subshell process-substitution






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 hours ago









      Sean AllredSean Allred

      8143816




      8143816






















          1 Answer
          1






          active

          oldest

          votes


















          7














          Those are the errors you get when trying to perform a process substitution in bash when the shell is running in POSIX mode. The bash shell does not support process substitutions in POSIX mode.



          bash will run in POSIX mode when either





          1. set -o posix has been used, or

          2. the shell is being invoked as sh.


          My hunch is that you have a script, test.sh, that you are running with sh test.sh or that has a #!/bin/sh hashbang line, and that your sh happens to be bash. Another possibility is that the script does not have #!-line at all, and it is being invoked by bash-as-sh in some other way.



          Instead, see to that your test.sh script is being invoked by bash.



          Example:



          $ cat script.sh
          echo hello




          $ cat test.sh
          var=$(bash <( cat script.sh ))
          printf 'var="%s"n' "$var"




          $ bash -o posix test.sh
          test.sh: command substitution: line 2: syntax error near unexpected token `('
          test.sh: command substitution: line 2: `bash <( cat script.sh ))'
          var=""




          $ bash test.sh
          var="hello"





          share|improve this answer


























          • Yep, I was invoking as sh – I didn't realize POSIX didn't support these features. Thanks! As a short follow-up, is there a way to ensure bash-as-sh (if invoked incorrectly) will not run in POSIX mode? In other words, what is the converse of set -o posix?

            – Sean Allred
            1 hour ago













          • @SeanAllred Since sh does not need to be bash and indeed, sh is often not bash, you should rather make sure that your scripts are executed by bash. A simple way to do this is to make the script executable and add #!/bin/bash as the first line in the script (adapt that to the correct path to the bash executable on your system). Then run it without specifying an explicit interpreter.

            – Kusalananda
            1 hour ago













          • Yes I can ensure that my tools use bash, via shebang or otherwise, but I'm concerned that those using the script I'm making will not invoke it correctly. Is there no converse to option posix? (Working under the assumption now that there's no means by which to accomplish my goal – without temp files – with only POSIX features.)

            – Sean Allred
            1 hour ago













          • @SeanAllred If you are sharing your script with others, it extra important to have a #!/bin/bash line! The sh shell on many Linux machines is dash, and that shell would not understand process substitutions however hard you tried. On my machine (OpenBSD), sh is pdksh, and that also can't be made to run process substitutions. What you are asking is the equivalent to how to make someone invoke a Python script with the correct Python interpreter even when they run it with a Ruby interpreter. If the user picks the wrong interpreter, then the documentation was not good enough.

            – Kusalananda
            1 hour ago













          • Well of course there will be a shebang line :-) but fair enough – I guess there's a limit to how much you can protect others from themselves. Thanks for your help!

            – Sean Allred
            1 hour ago













          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%2f502595%2fprocess-substitution-inside-a-subshell-to-set-a-variable%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          7














          Those are the errors you get when trying to perform a process substitution in bash when the shell is running in POSIX mode. The bash shell does not support process substitutions in POSIX mode.



          bash will run in POSIX mode when either





          1. set -o posix has been used, or

          2. the shell is being invoked as sh.


          My hunch is that you have a script, test.sh, that you are running with sh test.sh or that has a #!/bin/sh hashbang line, and that your sh happens to be bash. Another possibility is that the script does not have #!-line at all, and it is being invoked by bash-as-sh in some other way.



          Instead, see to that your test.sh script is being invoked by bash.



          Example:



          $ cat script.sh
          echo hello




          $ cat test.sh
          var=$(bash <( cat script.sh ))
          printf 'var="%s"n' "$var"




          $ bash -o posix test.sh
          test.sh: command substitution: line 2: syntax error near unexpected token `('
          test.sh: command substitution: line 2: `bash <( cat script.sh ))'
          var=""




          $ bash test.sh
          var="hello"





          share|improve this answer


























          • Yep, I was invoking as sh – I didn't realize POSIX didn't support these features. Thanks! As a short follow-up, is there a way to ensure bash-as-sh (if invoked incorrectly) will not run in POSIX mode? In other words, what is the converse of set -o posix?

            – Sean Allred
            1 hour ago













          • @SeanAllred Since sh does not need to be bash and indeed, sh is often not bash, you should rather make sure that your scripts are executed by bash. A simple way to do this is to make the script executable and add #!/bin/bash as the first line in the script (adapt that to the correct path to the bash executable on your system). Then run it without specifying an explicit interpreter.

            – Kusalananda
            1 hour ago













          • Yes I can ensure that my tools use bash, via shebang or otherwise, but I'm concerned that those using the script I'm making will not invoke it correctly. Is there no converse to option posix? (Working under the assumption now that there's no means by which to accomplish my goal – without temp files – with only POSIX features.)

            – Sean Allred
            1 hour ago













          • @SeanAllred If you are sharing your script with others, it extra important to have a #!/bin/bash line! The sh shell on many Linux machines is dash, and that shell would not understand process substitutions however hard you tried. On my machine (OpenBSD), sh is pdksh, and that also can't be made to run process substitutions. What you are asking is the equivalent to how to make someone invoke a Python script with the correct Python interpreter even when they run it with a Ruby interpreter. If the user picks the wrong interpreter, then the documentation was not good enough.

            – Kusalananda
            1 hour ago













          • Well of course there will be a shebang line :-) but fair enough – I guess there's a limit to how much you can protect others from themselves. Thanks for your help!

            – Sean Allred
            1 hour ago


















          7














          Those are the errors you get when trying to perform a process substitution in bash when the shell is running in POSIX mode. The bash shell does not support process substitutions in POSIX mode.



          bash will run in POSIX mode when either





          1. set -o posix has been used, or

          2. the shell is being invoked as sh.


          My hunch is that you have a script, test.sh, that you are running with sh test.sh or that has a #!/bin/sh hashbang line, and that your sh happens to be bash. Another possibility is that the script does not have #!-line at all, and it is being invoked by bash-as-sh in some other way.



          Instead, see to that your test.sh script is being invoked by bash.



          Example:



          $ cat script.sh
          echo hello




          $ cat test.sh
          var=$(bash <( cat script.sh ))
          printf 'var="%s"n' "$var"




          $ bash -o posix test.sh
          test.sh: command substitution: line 2: syntax error near unexpected token `('
          test.sh: command substitution: line 2: `bash <( cat script.sh ))'
          var=""




          $ bash test.sh
          var="hello"





          share|improve this answer


























          • Yep, I was invoking as sh – I didn't realize POSIX didn't support these features. Thanks! As a short follow-up, is there a way to ensure bash-as-sh (if invoked incorrectly) will not run in POSIX mode? In other words, what is the converse of set -o posix?

            – Sean Allred
            1 hour ago













          • @SeanAllred Since sh does not need to be bash and indeed, sh is often not bash, you should rather make sure that your scripts are executed by bash. A simple way to do this is to make the script executable and add #!/bin/bash as the first line in the script (adapt that to the correct path to the bash executable on your system). Then run it without specifying an explicit interpreter.

            – Kusalananda
            1 hour ago













          • Yes I can ensure that my tools use bash, via shebang or otherwise, but I'm concerned that those using the script I'm making will not invoke it correctly. Is there no converse to option posix? (Working under the assumption now that there's no means by which to accomplish my goal – without temp files – with only POSIX features.)

            – Sean Allred
            1 hour ago













          • @SeanAllred If you are sharing your script with others, it extra important to have a #!/bin/bash line! The sh shell on many Linux machines is dash, and that shell would not understand process substitutions however hard you tried. On my machine (OpenBSD), sh is pdksh, and that also can't be made to run process substitutions. What you are asking is the equivalent to how to make someone invoke a Python script with the correct Python interpreter even when they run it with a Ruby interpreter. If the user picks the wrong interpreter, then the documentation was not good enough.

            – Kusalananda
            1 hour ago













          • Well of course there will be a shebang line :-) but fair enough – I guess there's a limit to how much you can protect others from themselves. Thanks for your help!

            – Sean Allred
            1 hour ago
















          7












          7








          7







          Those are the errors you get when trying to perform a process substitution in bash when the shell is running in POSIX mode. The bash shell does not support process substitutions in POSIX mode.



          bash will run in POSIX mode when either





          1. set -o posix has been used, or

          2. the shell is being invoked as sh.


          My hunch is that you have a script, test.sh, that you are running with sh test.sh or that has a #!/bin/sh hashbang line, and that your sh happens to be bash. Another possibility is that the script does not have #!-line at all, and it is being invoked by bash-as-sh in some other way.



          Instead, see to that your test.sh script is being invoked by bash.



          Example:



          $ cat script.sh
          echo hello




          $ cat test.sh
          var=$(bash <( cat script.sh ))
          printf 'var="%s"n' "$var"




          $ bash -o posix test.sh
          test.sh: command substitution: line 2: syntax error near unexpected token `('
          test.sh: command substitution: line 2: `bash <( cat script.sh ))'
          var=""




          $ bash test.sh
          var="hello"





          share|improve this answer















          Those are the errors you get when trying to perform a process substitution in bash when the shell is running in POSIX mode. The bash shell does not support process substitutions in POSIX mode.



          bash will run in POSIX mode when either





          1. set -o posix has been used, or

          2. the shell is being invoked as sh.


          My hunch is that you have a script, test.sh, that you are running with sh test.sh or that has a #!/bin/sh hashbang line, and that your sh happens to be bash. Another possibility is that the script does not have #!-line at all, and it is being invoked by bash-as-sh in some other way.



          Instead, see to that your test.sh script is being invoked by bash.



          Example:



          $ cat script.sh
          echo hello




          $ cat test.sh
          var=$(bash <( cat script.sh ))
          printf 'var="%s"n' "$var"




          $ bash -o posix test.sh
          test.sh: command substitution: line 2: syntax error near unexpected token `('
          test.sh: command substitution: line 2: `bash <( cat script.sh ))'
          var=""




          $ bash test.sh
          var="hello"






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 1 hour ago

























          answered 1 hour ago









          KusalanandaKusalananda

          132k17250411




          132k17250411













          • Yep, I was invoking as sh – I didn't realize POSIX didn't support these features. Thanks! As a short follow-up, is there a way to ensure bash-as-sh (if invoked incorrectly) will not run in POSIX mode? In other words, what is the converse of set -o posix?

            – Sean Allred
            1 hour ago













          • @SeanAllred Since sh does not need to be bash and indeed, sh is often not bash, you should rather make sure that your scripts are executed by bash. A simple way to do this is to make the script executable and add #!/bin/bash as the first line in the script (adapt that to the correct path to the bash executable on your system). Then run it without specifying an explicit interpreter.

            – Kusalananda
            1 hour ago













          • Yes I can ensure that my tools use bash, via shebang or otherwise, but I'm concerned that those using the script I'm making will not invoke it correctly. Is there no converse to option posix? (Working under the assumption now that there's no means by which to accomplish my goal – without temp files – with only POSIX features.)

            – Sean Allred
            1 hour ago













          • @SeanAllred If you are sharing your script with others, it extra important to have a #!/bin/bash line! The sh shell on many Linux machines is dash, and that shell would not understand process substitutions however hard you tried. On my machine (OpenBSD), sh is pdksh, and that also can't be made to run process substitutions. What you are asking is the equivalent to how to make someone invoke a Python script with the correct Python interpreter even when they run it with a Ruby interpreter. If the user picks the wrong interpreter, then the documentation was not good enough.

            – Kusalananda
            1 hour ago













          • Well of course there will be a shebang line :-) but fair enough – I guess there's a limit to how much you can protect others from themselves. Thanks for your help!

            – Sean Allred
            1 hour ago





















          • Yep, I was invoking as sh – I didn't realize POSIX didn't support these features. Thanks! As a short follow-up, is there a way to ensure bash-as-sh (if invoked incorrectly) will not run in POSIX mode? In other words, what is the converse of set -o posix?

            – Sean Allred
            1 hour ago













          • @SeanAllred Since sh does not need to be bash and indeed, sh is often not bash, you should rather make sure that your scripts are executed by bash. A simple way to do this is to make the script executable and add #!/bin/bash as the first line in the script (adapt that to the correct path to the bash executable on your system). Then run it without specifying an explicit interpreter.

            – Kusalananda
            1 hour ago













          • Yes I can ensure that my tools use bash, via shebang or otherwise, but I'm concerned that those using the script I'm making will not invoke it correctly. Is there no converse to option posix? (Working under the assumption now that there's no means by which to accomplish my goal – without temp files – with only POSIX features.)

            – Sean Allred
            1 hour ago













          • @SeanAllred If you are sharing your script with others, it extra important to have a #!/bin/bash line! The sh shell on many Linux machines is dash, and that shell would not understand process substitutions however hard you tried. On my machine (OpenBSD), sh is pdksh, and that also can't be made to run process substitutions. What you are asking is the equivalent to how to make someone invoke a Python script with the correct Python interpreter even when they run it with a Ruby interpreter. If the user picks the wrong interpreter, then the documentation was not good enough.

            – Kusalananda
            1 hour ago













          • Well of course there will be a shebang line :-) but fair enough – I guess there's a limit to how much you can protect others from themselves. Thanks for your help!

            – Sean Allred
            1 hour ago



















          Yep, I was invoking as sh – I didn't realize POSIX didn't support these features. Thanks! As a short follow-up, is there a way to ensure bash-as-sh (if invoked incorrectly) will not run in POSIX mode? In other words, what is the converse of set -o posix?

          – Sean Allred
          1 hour ago







          Yep, I was invoking as sh – I didn't realize POSIX didn't support these features. Thanks! As a short follow-up, is there a way to ensure bash-as-sh (if invoked incorrectly) will not run in POSIX mode? In other words, what is the converse of set -o posix?

          – Sean Allred
          1 hour ago















          @SeanAllred Since sh does not need to be bash and indeed, sh is often not bash, you should rather make sure that your scripts are executed by bash. A simple way to do this is to make the script executable and add #!/bin/bash as the first line in the script (adapt that to the correct path to the bash executable on your system). Then run it without specifying an explicit interpreter.

          – Kusalananda
          1 hour ago







          @SeanAllred Since sh does not need to be bash and indeed, sh is often not bash, you should rather make sure that your scripts are executed by bash. A simple way to do this is to make the script executable and add #!/bin/bash as the first line in the script (adapt that to the correct path to the bash executable on your system). Then run it without specifying an explicit interpreter.

          – Kusalananda
          1 hour ago















          Yes I can ensure that my tools use bash, via shebang or otherwise, but I'm concerned that those using the script I'm making will not invoke it correctly. Is there no converse to option posix? (Working under the assumption now that there's no means by which to accomplish my goal – without temp files – with only POSIX features.)

          – Sean Allred
          1 hour ago







          Yes I can ensure that my tools use bash, via shebang or otherwise, but I'm concerned that those using the script I'm making will not invoke it correctly. Is there no converse to option posix? (Working under the assumption now that there's no means by which to accomplish my goal – without temp files – with only POSIX features.)

          – Sean Allred
          1 hour ago















          @SeanAllred If you are sharing your script with others, it extra important to have a #!/bin/bash line! The sh shell on many Linux machines is dash, and that shell would not understand process substitutions however hard you tried. On my machine (OpenBSD), sh is pdksh, and that also can't be made to run process substitutions. What you are asking is the equivalent to how to make someone invoke a Python script with the correct Python interpreter even when they run it with a Ruby interpreter. If the user picks the wrong interpreter, then the documentation was not good enough.

          – Kusalananda
          1 hour ago







          @SeanAllred If you are sharing your script with others, it extra important to have a #!/bin/bash line! The sh shell on many Linux machines is dash, and that shell would not understand process substitutions however hard you tried. On my machine (OpenBSD), sh is pdksh, and that also can't be made to run process substitutions. What you are asking is the equivalent to how to make someone invoke a Python script with the correct Python interpreter even when they run it with a Ruby interpreter. If the user picks the wrong interpreter, then the documentation was not good enough.

          – Kusalananda
          1 hour ago















          Well of course there will be a shebang line :-) but fair enough – I guess there's a limit to how much you can protect others from themselves. Thanks for your help!

          – Sean Allred
          1 hour ago







          Well of course there will be a shebang line :-) but fair enough – I guess there's a limit to how much you can protect others from themselves. Thanks for your help!

          – Sean Allred
          1 hour ago




















          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%2f502595%2fprocess-substitution-inside-a-subshell-to-set-a-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







          Popular posts from this blog

          Szabolcs (Ungheria) Altri progetti | Menu di navigazione48°10′14.56″N 21°29′33.14″E /...

          Discografia di Klaus Schulze Indice Album in studio | Album dal vivo | Singoli | Antologie | Colonne...

          How to make inet_server_addr() return localhost in spite of ::1/128RETURN NEXT in Postgres FunctionConnect to...