[BASH] probleem met newline

Pagina: 1
Acties:

  • GiLuX
  • Registratie: Juni 1999
  • Laatst online: 12-11-2025
ik heb een probleempje om met bash newlines te genereren.

kan het antwoord niet vinden met google.

bedoeling is dat vanuit een bash script een file wordt gegenereerd,
echter met onderstaand voorbeeld zijn in de gegenereerde file alle newlines weg.

code:
1
2
3
4
5
myvar="bla \"een string\""$'\n'\
"bli 1234"$'\n'\
"blow nogiets"$'\n'

echo $myvar > myfile


myfile:
code:
1
bla "een string" bli 1234 blow nogiets


als ik de 'normale' manier van newline escapen gebruik:
\n ipv $'\n'
dan krijg ik een gewone
n
te zien

echter, als ik dit op de commandline doe:
code:
1
echo "bla \"een string\""$'\n'"bli 1234"$'\n'"blow nogiets"$'\n'

dan krijg ik wel netjes newlines te zien:
code:
1
2
3
bla "een string"
bli 1234
blow nogiets


klaarblijkelijk gaan die newlines verloren bij het toewijzen aan myvar.
is hier iets aan te doen of zijn makkelijkere dingen voor?

is bash 2.05b.0

"I disagree with what you are saying, but I will defend to the death your right to say it." -- not clear who


  • ajvdvegt
  • Registratie: Maart 2000
  • Laatst online: 04-12-2025
Uit man bash:
If the -e option is given, interpretation of the following backslash-escaped characters is enabled.
code:
1
echo -e $myvar > myfile

I don't kill flies, but I like to mess with their minds. I hold them above globes. They freak out and yell "Whooa, I'm *way* too high." -- Bruce Baum


Verwijderd

probeer eens echo -e "bla \n bla \n bla"

  • GiLuX
  • Registratie: Juni 1999
  • Laatst online: 12-11-2025
ja, vergeten te melden,
dat had ik als eerste geprobeerd but no go

"I disagree with what you are saying, but I will defend to the death your right to say it." -- not clear who


  • GiLuX
  • Registratie: Juni 1999
  • Laatst online: 12-11-2025
hmmm,
werkt toch.

mischien had ik zo zitten klooien dat ik inmiddels $'\n' had getest en dat werkt niet..
overigens, \$'\n' werkt weer wel zie ik nu.

soms zie je het gewoon even niet meer... :o

tnx guys!

"I disagree with what you are saying, but I will defend to the death your right to say it." -- not clear who


Verwijderd

code:
1
2
3
4
5
6
#!/bin/sh
myvar="bla \"een string\"\n"\
"bli 1234\n"\
"blow nogiets\n"

echo -e $myvar > myfile


Dit werkt, klaar, uit.

  • GiLuX
  • Registratie: Juni 1999
  • Laatst online: 12-11-2025
just in dime?

"I disagree with what you are saying, but I will defend to the death your right to say it." -- not clear who

Pagina: 1