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

Is this Article About Possible Mirrored Universe Junk Science?

Select all columns except geometry using virtual layers

What is wrong with my use of "find -print0"?

Is it possible to methodically find the total of ways to read a given phrase making a stack?

How bad is a Computer Science course that doesn't teach Design Patterns?

How to wrap a figure in exam document?

What's the reason that we have different quantities of days each month?

Why don't you get burned by the wood benches in a sauna?

Fantasy series with a god hiding in plain sight as a manservant to a former king's brother and an undead pirate woman who runs a brothel

How unreachable are Jupiter's moons from Mars with the technology developed for going to Mars?

Boss asked me to sign a resignation paper without a date on it along with my new contract

Players preemptively rolling, even though their rolls are useless or are checking the wrong skills

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

Buying a "Used" Router

Is there any way to play D&D without a DM?

How to write Muḥammad ibn Mūsā al-Khwārizmī?

Why does a single AND gate need 60 transistors?

Color of alien seas

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

What does an unprocessed RAW file look like?

Including proofs of known theorems in master's thesis

Would water spill from a bowl in a Bag of Holding?

Is "accuse people to be racist" grammatical?

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



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 1 hour 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

          Щит и меч (фильм) Содержание Названия серий | Сюжет |...

          Венесуэла на летних Олимпийских играх 2000 Содержание Состав...

          Meter-Bus Содержание Параметры шины | Стандартизация |...