Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7399769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:06:13+00:00 2026-05-29T04:06:13+00:00

I have var="a b c" for i in $var do p=`echo -e $p’\n’$i` done

  • 0

I have

var="a b c"
for i in $var
do
   p=`echo -e $p'\n'$i`
done
echo $p

I want the last echo to print:

a
b
c

Notice that I want the variable p to contain newlines. How do I do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:06:13+00:00Added an answer on May 29, 2026 at 4:06 am

    Summary

    1. Inserting a new line in the source code

       p="${var1}
       ${var2}"
       echo "${p}"
      
    2. Using $'\n' (only Bash and Z shell)

       p="${var1}"$'\n'"${var2}"
       echo "${p}"
      
    3. Using echo -e to convert \n to a new line

       p="${var1}\n${var2}"
       echo -e "${p}"
      

    Details

    1. Inserting a new line in the source code

       var="a b c"
       for i in $var
       do
          p="$p
       $i"       # New line directly in the source code
       done
       echo "$p" # Double quotes required
                 # But -e not required
      

      Avoid extra leading newline

       var="a b c"
       first_loop=1
       for i in $var
       do
          (( $first_loop )) &&  # "((...))" is Bash specific
          p="$i"            ||  # First -> Set
          p="$p
       $i"                      # After -> Append
          unset first_loop
       done
       echo "$p"                # No need -e
      

      Using a function

       embed_newline()
       {
          local p="$1"
          shift
          for i in "$@"
          do
             p="$p
       $i"                      # Append
          done
          echo "$p"             # No need -e
       }
      
       var="a b c"
       p=$( embed_newline $var )  # Do not use double quotes "$var"
       echo "$p"
      
    2. Using $'\n' (less portable)

      bash and zsh interprets $'\n' as a new line.

       var="a b c"
       for i in $var
       do
          p="$p"$'\n'"$i"
       done
       echo "$p" # Double quotes required
                 # But -e not required
      

      Avoid extra leading newline

       var="a b c"
       first_loop=1
       for i in $var
       do
          (( $first_loop )) &&  # "((...))" is bash specific
          p="$i"            ||  # First -> Set
          p="$p"$'\n'"$i"       # After -> Append
          unset first_loop
       done
       echo "$p"                # No need -e
      

      Using a function

       embed_newline()
       {
          local p="$1"
          shift
          for i in "$@"
          do
             p="$p"$'\n'"$i"    # Append
          done
          echo "$p"             # No need -e
       }
      
       var="a b c"
       p=$( embed_newline $var )  # Do not use double quotes "$var"
       echo "$p"
      
    3. Using echo -e to convert \n to a new line

       p="${var1}\n${var2}"
       echo -e "${p}"
      

      echo -e interprets the two characters "\n" as a new line.

       var="a b c"
       first_loop=true
       for i in $var
       do
          p="$p\n$i"            # Append
          unset first_loop
       done
       echo -e "$p"             # Use -e
      

      Avoid extra leading newline

       var="a b c"
       first_loop=1
       for i in $var
       do
          (( $first_loop )) &&  # "((...))" is bash specific
          p="$i"            ||  # First -> Set
          p="$p\n$i"            # After -> Append
          unset first_loop
       done
       echo -e "$p"             # Use -e
      

      Using a function

       embed_newline()
       {
          local p="$1"
          shift
          for i in "$@"
          do
             p="$p\n$i"         # Append
          done
          echo -e "$p"          # Use -e
       }
      
       var="a b c"
       p=$( embed_newline $var )  # Do not use double quotes "$var"
       echo "$p"
      

      ⚠ Inserting "\n" in a string is not enough to insert a new line:
      "\n" are just two characters.

    The output is the same for all

    a
    b
    c
    

    Special thanks to contributors of this answer: kevinf, Gordon Davisson, l0b0, Dolda2000 and tripleee.


    • See also BinaryZebra’s answer, providing many details.
    • Abhijeet Rastogi’s answer and Dimitry’s answer explain how to avoid the for loop in the above Bash snippets.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an external JavaScript that contains: function random_imglink(){ var myimages=new Array() myimages[1]="http://sevir.sitegoz.com/jchs/Banner1.png" myimages[2]="http://sevir.sitegoz.com/jchs/banner2.png"
Let's say I have: var url="http://www.google.html"; how do I put the URL variable value
Suppose I have this code: var myArray = new Object(); myArray["firstname"] = "Bob"; myArray["lastname"]
I have a var that contains a full html page, including the head, html,
I am using JavaScript. I have var myobj = { "one": { "name": "paul",
I have two arrays: var array1 = ["A", "B", "C"]; var array2 = ["1",
I have a comma-separated string that I want to convert into an array, so
I have two arrays: var columns = ["Date", "Number", "Size", "Location", "Age"]; var rows
I have a varable var file = "testfile.txt"; I am using the following script
I have created a Python script that I want to run daily via a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.