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 4084734
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:28:40+00:00 2026-05-20T18:28:40+00:00

I want to assign the grep result to a variable for further use: lines=$(cat

  • 0

I want to assign the grep result to a variable for further use:

lines=$(cat abc.txt | grep "hello")

but seems newline characters are removed in the result, when I do

echo $lines

only one line is printed. How can I preserve newline characters, so when I echo $lines, it generates the same result as cat abc.txt | grep "hello" does.

  • 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-20T18:28:40+00:00Added an answer on May 20, 2026 at 6:28 pm

    You want to say

    echo "$lines"
    

    instead of

    echo $lines
    

    To elaborate:

    echo $lines means “Form a new command by replacing $lines with the contents of the variable named lines, splitting it up on whitespace to form zero or more new arguments to the echo command. For example:

    lines='1 2 3'
    echo $lines   # equivalent to "echo 1 2 3"
    lines='1   2   3'
    echo $lines   # also equivalent to "echo 1 2 3"
    lines="1
    2
    3"
    echo $lines   # also equivalent to "echo 1 2 3"
    

    All these examples are equivalent, because the shell ignores the specific kind of whitespace between the individual words stored in the variable lines. Actually, to be more precise, the shell splits the contents of the variable on the characters of the special IFS (Internal Field Separator) variable, which defaults (at least on my version of bash) to the three characters space, tab, and newline.

    echo "$lines", on the other hand, means to form a single new argument from the exact value of the variable lines.

    For more details, see the “Expansion” and “Word Splitting” sections of the bash manual page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to assign a member variable to the result of a lengthy block
I want to assign a string of characters to a variable but it says
I want to assign an HTML string to a JavaScript variable but it gives
I want to assign the decimal variable "trans" to the double variable "this.Opacity". decimal
I want to assign the array item into variable directly using groovy like this:
I just want to assign a variable to a function so it can be
i want to assign the linq result to a multidimensional array and the below
i want to assign multiple resources (engineers) to the same sprint. But, not able
I want to assign a string like: 22 33 to a variable like char*av[129]
I want to assign my variable [vPrenom_id_obtenu] by the value that I get in

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.