0

when I call env to see the environment variables it shows nicely with new lines:

$ env
SHELL=/bin/sh    
EDITOR=vi
PWD=/home/user
...

but when I want to store this to a file the new lines get discarded:

$ echo $(env) > root.env
SHELL=/bin/sh EDITOR=vi PWD=/home/user ...

What goes wrong? how can I store the env output with each item on a line?

DEKKER
  • 834
  • 8
  • 18
  • Try `env > root.env`. No need to use a subshell. – Stewart Jun 07 '22 at 10:57
  • @Stewart Well thanks that worked, but why echo fails then? – DEKKER Jun 07 '22 at 11:03
  • Typing it in an answer. But it's because the subshell returns line-by-line. Not just a single string of text containing newlines. – Stewart Jun 07 '22 at 11:08
  • It's the same word-splitting issue as [Why does echo of awk in a bash script remove line breaks?](https://unix.stackexchange.com/questions/306824/why-does-echo-of-awk-in-a-bash-script-remove-line-breaks) – steeldriver Jun 07 '22 at 11:23
  • and also see http://mywiki.wooledge.org/WordSplitting. But there's seldom, if ever a need to do `echo $(...)`, the `echo` and the command substitution just undo each other. – ilkkachu Jun 07 '22 at 11:47
  • [What is wrong with `echo $(stuff)`?](https://superuser.com/q/1352850/432690) – Kamil Maciorowski Jun 08 '22 at 20:00

0 Answers0