PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > backticks remove new line characters
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

backticks remove new line characters

Réponse
 
LinkBack Outils de la discussion
Vieux 17/05/2007, 18h32   #1
garhone
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut backticks remove new line characters

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

  Réponse avec citation
Vieux 17/05/2007, 18h45   #2
Klaus Alexander Seistrup
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Backticks remove new line characters

Garhone wrote:

> $ myvar=`cat test.txt`
> $ echo $myvar


Use
myvar="$(cat test.txt)"
echo "${myvar}"

Cheers,

--
Klaus Alexander Seistrup
http://klaus.seistrup.dk/
  Réponse avec citation
Vieux 17/05/2007, 18h59   #3
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: backticks remove new line characters

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.
  Réponse avec citation
Vieux 17/05/2007, 19h26   #4
Patrick
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: backticks remove new line characters

In news:1179423161.812555.282340@o5g2000hsb.googlegro ups.com,
garhone <cacheung@consumercontact.com> wrote:

> $ myvar=`cat test.txt`
> $ echo $myvar
> Hello World File


echo "${myvar}"

  Réponse avec citation
Vieux 17/05/2007, 19h37   #5
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: backticks remove new line characters

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
  Réponse avec citation
Vieux 17/05/2007, 23h06   #6
Exal de Jesus Garcia Carrillo
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: backticks remove new line characters

garhone wrote:

[...]

> $ echo $myvar


use instead this:

echo "${myvar}"

--
Spam protection:
replace the word `nospam' for exal
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 08h26.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11095 seconds with 14 queries