0
caract="A B"
map="bbb"
for line in $caract
do 
RES_QUERY=$caract" "$map$'\n'$RES_QUERY
done

echo $RES_QUERY

I get :

A B bbb A B bbb

instead of :

A B bbb 
A B bbb
Dilettant
  • 3,267
  • 3
  • 29
  • 29
  • 1
    `echo` converts newlines to spaces. – AlexP Nov 08 '17 at 13:49
  • What is your use-case here? for what purpose are you trying to use `RES_QUERY` here. It will be better to use arrays for string interpolation than a plain variable – Inian Nov 08 '17 at 13:51
  • BTW, it'd be considerably better form to have your expansions quoted. `RES_QUERY="$caract $map"$'\n'"$RES_QUERY"`. Doesn't make a difference on the right-hand side of an assignment, but avoids serious bugs in numerous other scenarios. – Charles Duffy Nov 08 '17 at 13:52
  • (and I *do* agree that you should probably be assembling an array, as opposed to a string containing literal newlines). – Charles Duffy Nov 08 '17 at 13:53
  • ...well, unless you *really* need to export it to the environment, which isn't possible for arrays. Assumption thus far has been that the phrase "environment variable" in the question has just been a misnomer. – Charles Duffy Nov 08 '17 at 13:53
  • As an aside -- see [Why you shouldn't read lines with `for`](http://mywiki.wooledge.org/DontReadLinesWithFor), and [BashFAQ #1](http://mywiki.wooledge.org/BashFAQ/001) ("How can I read a file (data stream, variable) line-by-line (and/or field-by-field?") for the best-practice alternative. – Charles Duffy Nov 08 '17 at 14:00

1 Answers1

2

Please try changing echo $RES_QUERY to echo "$RES_QUERY" to keep the new lines printed on screen.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Thank for you answer but i want that the modification included in RES_QUERY=$caract" "$map$'\n'$RES_QUERY automaticaly –  Nov 08 '17 at 13:49
  • 2
    @user6223604, you can't. Changing the assignment lets you arbitrarily change the variable's value, but there's nothing wrong with the value -- the **only** problem is with how you're printing it. – Charles Duffy Nov 08 '17 at 13:51