Unix Shell - Is there an echo in here?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2006 > Is there an echo in here?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Is there an echo in here?
Mr_Bill

2006-01-23, 7:52 am

I'm using GNU bash, version 3.00.16(1)-release (i586-suse-linux)

$1 is an eight line text file

#!/bin/bash

cat $1 # produces 8 lines.

the_file=$(cat $1)

echo $the_file # produces all the text with no line feeds

How do I get echo to preserve the line feeds?

Thanks,
Mr_Bill

PM

2006-01-23, 7:52 am

Mr_Bill wrote:
> I'm using GNU bash, version 3.00.16(1)-release (i586-suse-linux)
>
> $1 is an eight line text file
>
> #!/bin/bash
>
> cat $1 # produces 8 lines.
>
> the_file=$(cat $1)
>
> echo $the_file # produces all the text with no line feeds
>
> How do I get echo to preserve the line feeds?
>

Nothing to do with echo, RTFM:

% man bash
....
Command Substitution
Command substitution allows the output of a command to
replace the command name. There are two forms:

$(command)
or
`command`

Bash performs the expansion by executing command and replac-
ing the command substitution with the standard output of the
command, with any trailing newlines deleted. Embedded new-
lines are not deleted, but they may be removed during word
splitting. The command substitution $(cat file) can be
replaced by the equivalent but faster $(< file).
....
Stachu 'Dozzie' K.

2006-01-23, 7:52 am

On 23.01.2006, Mr_Bill <No_thanks@null_mail.org> wrote:
> I'm using GNU bash, version 3.00.16(1)-release (i586-suse-linux)
>
> $1 is an eight line text file
>
> #!/bin/bash
>
> cat $1 # produces 8 lines.
>
> the_file=$(cat $1)
>
> echo $the_file # produces all the text with no line feeds
>
> How do I get echo to preserve the line feeds?


man bash -> /Word Splitting

--
Feel free to correct my English
Stanislaw Klekot
Chris F.A. Johnson

2006-01-23, 7:52 am

On 2006-01-23, Mr_Bill wrote:
> I'm using GNU bash, version 3.00.16(1)-release (i586-suse-linux)
>
> $1 is an eight line text file
>
> #!/bin/bash
>
> cat $1 # produces 8 lines.
>
> the_file=$(cat $1)
>
> echo $the_file # produces all the text with no line feeds
>
> How do I get echo to preserve the line feeds?


echo "$the_file"

Better is:

printf "%s\n" "$the_file"


Note that trailing line-feeds will still be removed. You can get
around that with:

the_file=$(cat "$1"; printf ".")
the_file=${the_file%.}


If trailing linefeeds don't matter, this is a faster way:

the_file=$(< "$1")

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Michael Tosch

2006-01-23, 7:52 am

Mr_Bill wrote:
> I'm using GNU bash, version 3.00.16(1)-release (i586-suse-linux)
>
> $1 is an eight line text file
>
> #!/bin/bash
>
> cat $1 # produces 8 lines.
>
> the_file=$(cat $1)
>
> echo $the_file # produces all the text with no line feeds
>
> How do I get echo to preserve the line feeds?
>
> Thanks,
> Mr_Bill
>


Quote the variable:

echo "$the_file"


--
Michael Tosch @ hp : com
Mr_Bill

2006-01-23, 6:13 pm

> $1 is an eight line text file
>
> #!/bin/bash
>
> cat $1 # produces 8 lines.
> the_file=$(cat $1)
> echo $the_file # produces all the text with no line feeds
>
> How do I get echo to preserve the line feeds?


Thanks, everyone:

thefile=$(< "$1")
echo "$thefile"

Works like a champ!

Mr_Bill




Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com