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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:54:53+00:00 2026-05-16T05:54:53+00:00

$ cat isbndb.sample | wc -l 13 $ var=$(cat isbndb.sample); echo $var | wc

  • 0
$ cat isbndb.sample | wc -l
13
$ var=$(cat isbndb.sample); echo $var | wc -l
1

Why is the newline character missing when I assign the string to the variable? How can I keep the newline character from being converted into a space?

I am using bash.

  • 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-16T05:54:53+00:00Added an answer on May 16, 2026 at 5:54 am

    You have to quote the variable to preserve the newlines.

    $ var=$(cat isbndb.sample); echo "$var" | wc -l
    

    And cat is unnecessary in both cases:

    $ wc -l < isbndb.sample
    $ var=$(< isbndb.sample); echo "$var" | wc -l
    

    Edit:

    Bash normally strips extra trailing newlines from a file when it assigns its contents to a variable. You have to resort to some tricks to preserve them. Try this:

    IFS='' read -d '' var < isbndb.sample; echo "$var" | wc -l
    

    Setting IFS to null prevents the file from being split on the newlines and setting the delimiter for read to null makes it accept the file until the end of file.

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

Sidebar

Related Questions

$ cat Const.java public class Const { String Const(String hello) { return hello; }
$ cat read.sh #!bin/bash // how can I read the columnwise data to awk-script?
How can I cat a remote file? Currently, it works for local files only.
bash-3.00$ cat arr.bash #!/bin/bash declare -a myarray myarray[2]=two myarray[5]=five echo ${#myarray[*]} echo ${#myarray[@]} bash-3.00$
$ cat fromhere.sh #!/bin/bash FROMHERE=10 for i in $(seq $FROMHERE 1) do echo $i
a = ['cat','dog'] random.choice(a) How can I choose a random word, and pull out
When I ran git cat-file --batch on a commit, it output 'missing...', but the
cat a.txt | xargs -I % echo % In the example above, xargs takes
Is there a command like cat in linux which can return a specified quantity
I tried the following code unsuccessfully cat pbcopy >> newFile How can you save

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.