|
Home > Archive > Unix Shell > May 2007 > backticks remove new line characters
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 |
backticks remove new line characters
|
|
| garhone 2007-05-17, 1:18 pm |
| Hi there,
How can I get the contents of a file into a variable, newlines
included. I've tried
$ cat test.txt
Hello
World
File
$ myvar=`cat test.txt`
$ echo $myvar
Hello World File
cat alone will display the file, newlines included, but using
backticks and assigning the output to a variable strips the newline
characters ?!
Thanks in advance,
C
| |
| Klaus Alexander Seistrup 2007-05-17, 1:18 pm |
| Garhone wrote:
> $ myvar=`cat test.txt`
> $ echo $myvar
Use
myvar="$(cat test.txt)"
echo "${myvar}"
Cheers,
--
Klaus Alexander Seistrup
http://klaus.seistrup.dk/
| |
| Bill Marcum 2007-05-17, 1:18 pm |
| On 17 May 2007 10:32:41 -0700, garhone
<cacheung@consumercontact.com> wrote:
>
>
> Hi there,
>
> How can I get the contents of a file into a variable, newlines
> included. I've tried
>
> $ cat test.txt
> Hello
> World
> File
> $ myvar=`cat test.txt`
> $ echo $myvar
> Hello World File
>
> cat alone will display the file, newlines included, but using
> backticks and assigning the output to a variable strips the newline
> characters ?!
>
echo "$myvar"
--
I watch television because you don't know what it will do if you leave it
in the room alone.
| |
| Patrick 2007-05-17, 7:18 pm |
| In news:1179423161.812555.282340@o5g2000hsb.googlegroups.com,
garhone <cacheung@consumercontact.com> wrote:
> $ myvar=`cat test.txt`
> $ echo $myvar
> Hello World File
echo "${myvar}"
| |
| Chris F.A. Johnson 2007-05-17, 7:18 pm |
| On 2007-05-17, garhone wrote:
> Hi there,
>
> How can I get the contents of a file into a variable, newlines
> included. I've tried
>
> $ cat test.txt
> Hello
> World
> File
> $ myvar=`cat test.txt`
> $ echo $myvar
> Hello World File
>
> cat alone will display the file, newlines included, but using
> backticks and assigning the output to a variable strips the newline
> characters ?!
Echo prints its arguments separated by spaces. To preserve the
newlines, quote the variable:
echo "$myvar"
Or, better:
printf "%s\n" "$myvar"
Note, however, that command substition removes trailing newlines.
Do this to preserve them:
myvar=`cat test.txt; echo .`
myvar=${myvar%.}
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
| Exal de Jesus Garcia Carrillo 2007-05-17, 7:18 pm |
| garhone wrote:
[...]
> $ echo $myvar
use instead this:
echo "${myvar}"
--
Spam protection:
replace the word `nospam' for exal
|
|
|
|
|