bash - add blank line to heredoc via variable

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












If I am using this scenario in a script:



#!/bin/bash

addvline=$(if [ "$1" ]; then echo "$1"; echo; fi)

cat << EOF
this is the first line
$addvline
this is the last line
EOF


if $1 is emty I get a blank line.

But how can I add a blank line after $1 for the case it is not emty?



So in the case running the script like:
bash script.sh hello



I would get:



this is the first line
hello

this is the last line


I tried to achieve this with using a second echo in the if statement, but the newline does not get passed.










share|improve this question

























    up vote
    0
    down vote

    favorite












    If I am using this scenario in a script:



    #!/bin/bash

    addvline=$(if [ "$1" ]; then echo "$1"; echo; fi)

    cat << EOF
    this is the first line
    $addvline
    this is the last line
    EOF


    if $1 is emty I get a blank line.

    But how can I add a blank line after $1 for the case it is not emty?



    So in the case running the script like:
    bash script.sh hello



    I would get:



    this is the first line
    hello

    this is the last line


    I tried to achieve this with using a second echo in the if statement, but the newline does not get passed.










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      If I am using this scenario in a script:



      #!/bin/bash

      addvline=$(if [ "$1" ]; then echo "$1"; echo; fi)

      cat << EOF
      this is the first line
      $addvline
      this is the last line
      EOF


      if $1 is emty I get a blank line.

      But how can I add a blank line after $1 for the case it is not emty?



      So in the case running the script like:
      bash script.sh hello



      I would get:



      this is the first line
      hello

      this is the last line


      I tried to achieve this with using a second echo in the if statement, but the newline does not get passed.










      share|improve this question













      If I am using this scenario in a script:



      #!/bin/bash

      addvline=$(if [ "$1" ]; then echo "$1"; echo; fi)

      cat << EOF
      this is the first line
      $addvline
      this is the last line
      EOF


      if $1 is emty I get a blank line.

      But how can I add a blank line after $1 for the case it is not emty?



      So in the case running the script like:
      bash script.sh hello



      I would get:



      this is the first line
      hello

      this is the last line


      I tried to achieve this with using a second echo in the if statement, but the newline does not get passed.







      bash variable cat here-document






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 1 '17 at 16:23









      nath

      633421




      633421




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Let if decide to set your variable content not to use command substitution.



          if [ "$1" ]; then addvline=$1$'n'; fi


          Then:



          #!/bin/bash
          if [ "$1" ]; then addvline=$1$'n'; fi
          cat << EOF
          this is the first line
          $addvline
          this is the last line
          EOF





          share|improve this answer



























            up vote
            1
            down vote













            There are several solutions to this. First, let's create a variable that contains a newline to be used later (in bash):



            nl=$'n'


            then it could either be used to construct the variable to be printed:



            #!/bin/bash
            nl=$'n'
            if [ "$1" ]; then
            addvline="$1$nl"
            else
            addvline=""
            fi

            cat << EOF
            this is the first line
            $addvline
            this is the last line
            EOF


            Or you could avoid the if entirely if you use the correct parameter expansion:



            #!/bin/bash
            nl=$'n'
            addvline="$1:+$1$nl"

            cat << EOF
            this is the first line
            $addvline
            this is the last line
            EOF


            Or, in one simpler code:



            #!/bin/bash
            nl=$'n'

            cat << EOF
            this is the first line
            $1:+$1$nl
            this is the last line
            EOF





            share|improve this answer






















              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',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              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%2f395484%2fbash-add-blank-line-to-heredoc-via-variable%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote



              accepted










              Let if decide to set your variable content not to use command substitution.



              if [ "$1" ]; then addvline=$1$'n'; fi


              Then:



              #!/bin/bash
              if [ "$1" ]; then addvline=$1$'n'; fi
              cat << EOF
              this is the first line
              $addvline
              this is the last line
              EOF





              share|improve this answer
























                up vote
                1
                down vote



                accepted










                Let if decide to set your variable content not to use command substitution.



                if [ "$1" ]; then addvline=$1$'n'; fi


                Then:



                #!/bin/bash
                if [ "$1" ]; then addvline=$1$'n'; fi
                cat << EOF
                this is the first line
                $addvline
                this is the last line
                EOF





                share|improve this answer






















                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  Let if decide to set your variable content not to use command substitution.



                  if [ "$1" ]; then addvline=$1$'n'; fi


                  Then:



                  #!/bin/bash
                  if [ "$1" ]; then addvline=$1$'n'; fi
                  cat << EOF
                  this is the first line
                  $addvline
                  this is the last line
                  EOF





                  share|improve this answer












                  Let if decide to set your variable content not to use command substitution.



                  if [ "$1" ]; then addvline=$1$'n'; fi


                  Then:



                  #!/bin/bash
                  if [ "$1" ]; then addvline=$1$'n'; fi
                  cat << EOF
                  this is the first line
                  $addvline
                  this is the last line
                  EOF






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 1 '17 at 17:35









                  αғsнιη

                  15.7k92563




                  15.7k92563






















                      up vote
                      1
                      down vote













                      There are several solutions to this. First, let's create a variable that contains a newline to be used later (in bash):



                      nl=$'n'


                      then it could either be used to construct the variable to be printed:



                      #!/bin/bash
                      nl=$'n'
                      if [ "$1" ]; then
                      addvline="$1$nl"
                      else
                      addvline=""
                      fi

                      cat << EOF
                      this is the first line
                      $addvline
                      this is the last line
                      EOF


                      Or you could avoid the if entirely if you use the correct parameter expansion:



                      #!/bin/bash
                      nl=$'n'
                      addvline="$1:+$1$nl"

                      cat << EOF
                      this is the first line
                      $addvline
                      this is the last line
                      EOF


                      Or, in one simpler code:



                      #!/bin/bash
                      nl=$'n'

                      cat << EOF
                      this is the first line
                      $1:+$1$nl
                      this is the last line
                      EOF





                      share|improve this answer


























                        up vote
                        1
                        down vote













                        There are several solutions to this. First, let's create a variable that contains a newline to be used later (in bash):



                        nl=$'n'


                        then it could either be used to construct the variable to be printed:



                        #!/bin/bash
                        nl=$'n'
                        if [ "$1" ]; then
                        addvline="$1$nl"
                        else
                        addvline=""
                        fi

                        cat << EOF
                        this is the first line
                        $addvline
                        this is the last line
                        EOF


                        Or you could avoid the if entirely if you use the correct parameter expansion:



                        #!/bin/bash
                        nl=$'n'
                        addvline="$1:+$1$nl"

                        cat << EOF
                        this is the first line
                        $addvline
                        this is the last line
                        EOF


                        Or, in one simpler code:



                        #!/bin/bash
                        nl=$'n'

                        cat << EOF
                        this is the first line
                        $1:+$1$nl
                        this is the last line
                        EOF





                        share|improve this answer
























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          There are several solutions to this. First, let's create a variable that contains a newline to be used later (in bash):



                          nl=$'n'


                          then it could either be used to construct the variable to be printed:



                          #!/bin/bash
                          nl=$'n'
                          if [ "$1" ]; then
                          addvline="$1$nl"
                          else
                          addvline=""
                          fi

                          cat << EOF
                          this is the first line
                          $addvline
                          this is the last line
                          EOF


                          Or you could avoid the if entirely if you use the correct parameter expansion:



                          #!/bin/bash
                          nl=$'n'
                          addvline="$1:+$1$nl"

                          cat << EOF
                          this is the first line
                          $addvline
                          this is the last line
                          EOF


                          Or, in one simpler code:



                          #!/bin/bash
                          nl=$'n'

                          cat << EOF
                          this is the first line
                          $1:+$1$nl
                          this is the last line
                          EOF





                          share|improve this answer














                          There are several solutions to this. First, let's create a variable that contains a newline to be used later (in bash):



                          nl=$'n'


                          then it could either be used to construct the variable to be printed:



                          #!/bin/bash
                          nl=$'n'
                          if [ "$1" ]; then
                          addvline="$1$nl"
                          else
                          addvline=""
                          fi

                          cat << EOF
                          this is the first line
                          $addvline
                          this is the last line
                          EOF


                          Or you could avoid the if entirely if you use the correct parameter expansion:



                          #!/bin/bash
                          nl=$'n'
                          addvline="$1:+$1$nl"

                          cat << EOF
                          this is the first line
                          $addvline
                          this is the last line
                          EOF


                          Or, in one simpler code:



                          #!/bin/bash
                          nl=$'n'

                          cat << EOF
                          this is the first line
                          $1:+$1$nl
                          this is the last line
                          EOF






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Oct 1 '17 at 19:44









                          Jeff Schaller

                          32.3k849109




                          32.3k849109










                          answered Oct 1 '17 at 17:51









                          Arrow

                          2,400218




                          2,400218



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395484%2fbash-add-blank-line-to-heredoc-via-variable%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay