AWK: why does $(cat) work for stdin, but $* doesn't?












8















echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $(cat) }"


The above syntax works fine with the calculated result '1337'.



echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $* }"


But the above syntax doesn't work, though there's no error.



Plz advise.










share|improve this question





























    8















    echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $(cat) }"


    The above syntax works fine with the calculated result '1337'.



    echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $* }"


    But the above syntax doesn't work, though there's no error.



    Plz advise.










    share|improve this question



























      8












      8








      8


      1






      echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $(cat) }"


      The above syntax works fine with the calculated result '1337'.



      echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $* }"


      But the above syntax doesn't work, though there's no error.



      Plz advise.










      share|improve this question
















      echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $(cat) }"


      The above syntax works fine with the calculated result '1337'.



      echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $* }"


      But the above syntax doesn't work, though there's no error.



      Plz advise.







      bash awk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 9:46









      heemayl

      66.2k8138211




      66.2k8138211










      asked Jan 2 at 1:31









      user58029user58029

      615




      615






















          1 Answer
          1






          active

          oldest

          votes


















          13














          The $(command) syntax will return the output of command. Here, you are using the very simple cat program whose only job is to copy everything from standard input (stdin) into standard output (stdout). Since you are running the awk script inside double quotes, the $(cat) is expanded by the shell before the awk script is run, so it reads the echo output into its stdin and duly copies it to its stdout. This is then passed to the awk script. You can see this in action with set -x:



          $ set -x
          $ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $(cat) }"
          + echo '((3+(2^3)) * 34^2 / 9)-75.89'
          ++ cat
          + awk 'BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }'
          1337


          So, awk is actually running BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }' which returns 1337.



          Now, the $* is a special shell variable that expands to all the positional parameters given to a shell script (see man bash):



             *      Expands to the positional parameters, starting from one.  When the expan‐
          sion is not within double quotes, each positional parameter expands to a
          separate word. In contexts where it is performed, those words are sub‐
          ject to further word splitting and pathname expansion. When the expan‐
          sion occurs within double quotes, it expands to a single word with the
          value of each parameter separated by the first character of the IFS spe‐
          cial variable. That is, "$*" is equivalent to "$1c$2c...", where c is
          the first character of the value of the IFS variable. If IFS is unset,
          the parameters are separated by spaces. If IFS is null, the parameters
          are joined without intervening separators.


          However, this variable is empty here. Therefore, the awk script becomes:



          $ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $* }"
          + awk 'BEGIN{ print }'
          + echo '((3+(2^3)) * 34^2 / 9)-75.89'


          The $* expands to an empty string, and awk is told to print an empty string, and this is why you get no output.





          You might want to just use bc instead:



          $ echo '((3+(2^3)) * 34^2 / 9)-75.89' | bc
          1336.11





          share|improve this answer


























          • Should probably use bc -l, otherwise you'll get the discrepancy you've posted above (where the result of the division has been truncated).

            – cmbuckley
            Jan 2 at 15:25











          • @cmbuckley I was trying to get it to print 1337 by playing around with scale= (I assume the OP wants to play with leetspeak) but I couldn't find a way. bc -l returns 1336.99888888888888888888 on my system.

            – terdon
            Jan 2 at 15:40











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "89"
          };
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1106132%2fawk-why-does-cat-work-for-stdin-but-doesnt%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









          13














          The $(command) syntax will return the output of command. Here, you are using the very simple cat program whose only job is to copy everything from standard input (stdin) into standard output (stdout). Since you are running the awk script inside double quotes, the $(cat) is expanded by the shell before the awk script is run, so it reads the echo output into its stdin and duly copies it to its stdout. This is then passed to the awk script. You can see this in action with set -x:



          $ set -x
          $ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $(cat) }"
          + echo '((3+(2^3)) * 34^2 / 9)-75.89'
          ++ cat
          + awk 'BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }'
          1337


          So, awk is actually running BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }' which returns 1337.



          Now, the $* is a special shell variable that expands to all the positional parameters given to a shell script (see man bash):



             *      Expands to the positional parameters, starting from one.  When the expan‐
          sion is not within double quotes, each positional parameter expands to a
          separate word. In contexts where it is performed, those words are sub‐
          ject to further word splitting and pathname expansion. When the expan‐
          sion occurs within double quotes, it expands to a single word with the
          value of each parameter separated by the first character of the IFS spe‐
          cial variable. That is, "$*" is equivalent to "$1c$2c...", where c is
          the first character of the value of the IFS variable. If IFS is unset,
          the parameters are separated by spaces. If IFS is null, the parameters
          are joined without intervening separators.


          However, this variable is empty here. Therefore, the awk script becomes:



          $ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $* }"
          + awk 'BEGIN{ print }'
          + echo '((3+(2^3)) * 34^2 / 9)-75.89'


          The $* expands to an empty string, and awk is told to print an empty string, and this is why you get no output.





          You might want to just use bc instead:



          $ echo '((3+(2^3)) * 34^2 / 9)-75.89' | bc
          1336.11





          share|improve this answer


























          • Should probably use bc -l, otherwise you'll get the discrepancy you've posted above (where the result of the division has been truncated).

            – cmbuckley
            Jan 2 at 15:25











          • @cmbuckley I was trying to get it to print 1337 by playing around with scale= (I assume the OP wants to play with leetspeak) but I couldn't find a way. bc -l returns 1336.99888888888888888888 on my system.

            – terdon
            Jan 2 at 15:40
















          13














          The $(command) syntax will return the output of command. Here, you are using the very simple cat program whose only job is to copy everything from standard input (stdin) into standard output (stdout). Since you are running the awk script inside double quotes, the $(cat) is expanded by the shell before the awk script is run, so it reads the echo output into its stdin and duly copies it to its stdout. This is then passed to the awk script. You can see this in action with set -x:



          $ set -x
          $ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $(cat) }"
          + echo '((3+(2^3)) * 34^2 / 9)-75.89'
          ++ cat
          + awk 'BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }'
          1337


          So, awk is actually running BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }' which returns 1337.



          Now, the $* is a special shell variable that expands to all the positional parameters given to a shell script (see man bash):



             *      Expands to the positional parameters, starting from one.  When the expan‐
          sion is not within double quotes, each positional parameter expands to a
          separate word. In contexts where it is performed, those words are sub‐
          ject to further word splitting and pathname expansion. When the expan‐
          sion occurs within double quotes, it expands to a single word with the
          value of each parameter separated by the first character of the IFS spe‐
          cial variable. That is, "$*" is equivalent to "$1c$2c...", where c is
          the first character of the value of the IFS variable. If IFS is unset,
          the parameters are separated by spaces. If IFS is null, the parameters
          are joined without intervening separators.


          However, this variable is empty here. Therefore, the awk script becomes:



          $ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $* }"
          + awk 'BEGIN{ print }'
          + echo '((3+(2^3)) * 34^2 / 9)-75.89'


          The $* expands to an empty string, and awk is told to print an empty string, and this is why you get no output.





          You might want to just use bc instead:



          $ echo '((3+(2^3)) * 34^2 / 9)-75.89' | bc
          1336.11





          share|improve this answer


























          • Should probably use bc -l, otherwise you'll get the discrepancy you've posted above (where the result of the division has been truncated).

            – cmbuckley
            Jan 2 at 15:25











          • @cmbuckley I was trying to get it to print 1337 by playing around with scale= (I assume the OP wants to play with leetspeak) but I couldn't find a way. bc -l returns 1336.99888888888888888888 on my system.

            – terdon
            Jan 2 at 15:40














          13












          13








          13







          The $(command) syntax will return the output of command. Here, you are using the very simple cat program whose only job is to copy everything from standard input (stdin) into standard output (stdout). Since you are running the awk script inside double quotes, the $(cat) is expanded by the shell before the awk script is run, so it reads the echo output into its stdin and duly copies it to its stdout. This is then passed to the awk script. You can see this in action with set -x:



          $ set -x
          $ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $(cat) }"
          + echo '((3+(2^3)) * 34^2 / 9)-75.89'
          ++ cat
          + awk 'BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }'
          1337


          So, awk is actually running BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }' which returns 1337.



          Now, the $* is a special shell variable that expands to all the positional parameters given to a shell script (see man bash):



             *      Expands to the positional parameters, starting from one.  When the expan‐
          sion is not within double quotes, each positional parameter expands to a
          separate word. In contexts where it is performed, those words are sub‐
          ject to further word splitting and pathname expansion. When the expan‐
          sion occurs within double quotes, it expands to a single word with the
          value of each parameter separated by the first character of the IFS spe‐
          cial variable. That is, "$*" is equivalent to "$1c$2c...", where c is
          the first character of the value of the IFS variable. If IFS is unset,
          the parameters are separated by spaces. If IFS is null, the parameters
          are joined without intervening separators.


          However, this variable is empty here. Therefore, the awk script becomes:



          $ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $* }"
          + awk 'BEGIN{ print }'
          + echo '((3+(2^3)) * 34^2 / 9)-75.89'


          The $* expands to an empty string, and awk is told to print an empty string, and this is why you get no output.





          You might want to just use bc instead:



          $ echo '((3+(2^3)) * 34^2 / 9)-75.89' | bc
          1336.11





          share|improve this answer















          The $(command) syntax will return the output of command. Here, you are using the very simple cat program whose only job is to copy everything from standard input (stdin) into standard output (stdout). Since you are running the awk script inside double quotes, the $(cat) is expanded by the shell before the awk script is run, so it reads the echo output into its stdin and duly copies it to its stdout. This is then passed to the awk script. You can see this in action with set -x:



          $ set -x
          $ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $(cat) }"
          + echo '((3+(2^3)) * 34^2 / 9)-75.89'
          ++ cat
          + awk 'BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }'
          1337


          So, awk is actually running BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }' which returns 1337.



          Now, the $* is a special shell variable that expands to all the positional parameters given to a shell script (see man bash):



             *      Expands to the positional parameters, starting from one.  When the expan‐
          sion is not within double quotes, each positional parameter expands to a
          separate word. In contexts where it is performed, those words are sub‐
          ject to further word splitting and pathname expansion. When the expan‐
          sion occurs within double quotes, it expands to a single word with the
          value of each parameter separated by the first character of the IFS spe‐
          cial variable. That is, "$*" is equivalent to "$1c$2c...", where c is
          the first character of the value of the IFS variable. If IFS is unset,
          the parameters are separated by spaces. If IFS is null, the parameters
          are joined without intervening separators.


          However, this variable is empty here. Therefore, the awk script becomes:



          $ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $* }"
          + awk 'BEGIN{ print }'
          + echo '((3+(2^3)) * 34^2 / 9)-75.89'


          The $* expands to an empty string, and awk is told to print an empty string, and this is why you get no output.





          You might want to just use bc instead:



          $ echo '((3+(2^3)) * 34^2 / 9)-75.89' | bc
          1336.11






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 3:05

























          answered Jan 2 at 1:54









          terdonterdon

          65.3k12138218




          65.3k12138218













          • Should probably use bc -l, otherwise you'll get the discrepancy you've posted above (where the result of the division has been truncated).

            – cmbuckley
            Jan 2 at 15:25











          • @cmbuckley I was trying to get it to print 1337 by playing around with scale= (I assume the OP wants to play with leetspeak) but I couldn't find a way. bc -l returns 1336.99888888888888888888 on my system.

            – terdon
            Jan 2 at 15:40



















          • Should probably use bc -l, otherwise you'll get the discrepancy you've posted above (where the result of the division has been truncated).

            – cmbuckley
            Jan 2 at 15:25











          • @cmbuckley I was trying to get it to print 1337 by playing around with scale= (I assume the OP wants to play with leetspeak) but I couldn't find a way. bc -l returns 1336.99888888888888888888 on my system.

            – terdon
            Jan 2 at 15:40

















          Should probably use bc -l, otherwise you'll get the discrepancy you've posted above (where the result of the division has been truncated).

          – cmbuckley
          Jan 2 at 15:25





          Should probably use bc -l, otherwise you'll get the discrepancy you've posted above (where the result of the division has been truncated).

          – cmbuckley
          Jan 2 at 15:25













          @cmbuckley I was trying to get it to print 1337 by playing around with scale= (I assume the OP wants to play with leetspeak) but I couldn't find a way. bc -l returns 1336.99888888888888888888 on my system.

          – terdon
          Jan 2 at 15:40





          @cmbuckley I was trying to get it to print 1337 by playing around with scale= (I assume the OP wants to play with leetspeak) but I couldn't find a way. bc -l returns 1336.99888888888888888888 on my system.

          – terdon
          Jan 2 at 15:40


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Ask Ubuntu!


          • 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%2faskubuntu.com%2fquestions%2f1106132%2fawk-why-does-cat-work-for-stdin-but-doesnt%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

          flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

          Mangá

          Eduardo VII do Reino Unido