Bash command file

Bash command file
  • Public
By Ian Stewart 238 days ago Comments (2)

DownloadMaps.sh

Comments

  • Ian Stewart 236 days ago

    Suggestion by Lawrence on improving this bash file...

    Just a couple of thoughts:
     
    * Instead of
     
        echo line1 >file
        echo line2 >>file
        echo line3 >>file
        ...etc...
     
      How about
     
        {
          echo line1
          echo line2
          echo line3
          ...etc...
        } >file
     
    * It’s easy to fix handling of the case where the output path might
      contain spaces: just put some double quotes in the right places.

     

  • Ian Stewart 236 days ago

    Suggestion from Daniel on improving the bash file...

           {
             echo line1
             echo line2
             echo line3
             ...etc...
           } >file


    “Here documents” in bash are also useful for this kind of work. In many ways, they’re better, because they preserve the formatting of the document completely:

    #!/bin/bash
    foo=“Some Variable"
    cat > file << EOF
    verbatim text here
       more verbatim text here, with preceding whitespace preserved
    no echo command needed
    expand some variable here: $foo
    EOF

    will produce a file that contains everything between the two EOFs exactly. It will still expand the contents of $foo out appropriately

    $ ./heredoctest.sh
    $ cat ./file
    verbatim text here
       more verbatim text here, with preceding whitespace preserved
    no echo command needed
    expand some variable here: Some Variable

    More on heredocs here.