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

  • Home
  • SEARCH
  • 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 8810517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:08:17+00:00 2026-06-14T03:08:17+00:00

Suppose we have variable test which contains newline character, for example, \n . If

  • 0

Suppose we have variable “test” which contains newline character, for example, \n. If I use the following command:

echo "$test" | grep '\n'

the result is not what I expect. The above grep command only search the test string whether contains character ‘n’ rather than newline since ‘\’ escape ‘n’ character.

How should I write grep command to search the newline character in specific string?
FYI, the following is not right too.

echo "$test" | grep '^.*$'
  • 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-06-14T03:08:18+00:00Added an answer on June 14, 2026 at 3:08 am

    By using the -c option of grep to count the lines that match this can be achieved. Note the $ character matches end of lines not \n also see how the double quotes around $test are important for preserving the line breaks.

    test="one
    two
    three"
    
    echo $test | grep -c '$'
    1
    
    echo "$test" | grep -c '$'
    3
    

    You could also test against ^ the start of the line or .* anything, or as in your question, the whole line ^.*$ by using the -c option.


    How about wc for testing multi-line variables. wc -l prints the newline count:

    echo "$test" | wc -l
    3
    

    Aswell as newlines you can also use wc to count characters and words in a file (or variable/stdout) with wc -m and wc -w respectively.


    Or how about using tr to replace \n with a unique character not contained in the variable for instance:

    echo "$test" | tr '\n' '_'
    one_two_three_
    

    You can then grep for the replaced character, in this case _


    Or even using cat -E

    echo "$test" | cat -E
    one$
    two$
    three$
    

    cat -E or --show-ends displays $ at the end of each line.

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

Sidebar

Related Questions

Suppose I have a program that initializes a global variable for use by threads,
Suppose I have variable x in bash. How can I test if it's some
Suppose I have a function: function test(){ //should I define global variable here //like
Let's suppose I have following two variables: arg1=5 count5=test Now, I want to able
Suppose I have the following classes: public class Test { public static void main(String[]
Suppose i have a LPSTR variable. How do i free the memory after using
Suppose I have a static variable declared inside a function in C. If I
Suppose I have a text variable $$string . How can I write a boolean
Suppose that I have an integer variable in a class, and this variable may
Suppose I have two classes that both contain a static variable, XmlTag. The second

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.