Uploaded image for project: 'Product Technical Learning'
  1. Product Technical Learning
  2. PTL-6561

RH254-284, Chap.12 bash RFE: improve $* / $@ demo script

XMLWordPrintable

    • 12

      URL:
      Reporter RHNID:
      Section: -
      Language:
      Workaround:

      Description: As it stands, now, the code on pp 353 is as follows:

      [student@serverX bin]$ cat showargs
      #!/bin/bash

      for ARG in "$*"; do
      echo $ARG
      done

      [student@serverX bin]$ ./showargs 1 2 3
      1 2 3

      [student@serverX bin]$ cat showargs
      #!/bin/bash

      for ARG in "$@"; do
      echo $ARG
      done

      [student@serverX bin]$ ./showargs 1 2 3
      1
      2
      3

      My students didn't really get what was going on here. And I don't blame them – this doesn't really prove anything.

      The improvement I would suggest: pass some quoted arguments which contain spaces to showargs, e.g.:

      [student@serverX bin]$ cat showargs
      #!/bin/bash

      for ARG in "$*"; do
      echo $ARG
      done

      [student@serverX bin]$ ./showargs "argument 1" 2 "arg 3"
      argument 1 2 arg 3

      [student@serverX bin]$ cat showargs
      #!/bin/bash

      for ARG in "$@"; do
      echo $ARG
      done

      [student@serverX bin]$ ./showargs "argument 1" 2 "arg 3"
      argument 1
      2
      arg 3

      I also think the two sentences on pp 352 starting with "When $* is used, all of the arguments are seen as as single word" are technically wrong, but honestly less important than a good demonstration.

            bsivasub@redhat.com Buvanesh Kumar
            ryanaroha Ryan Aroha (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: