Score:0

bash concatenate string in loop results in empty string

mx flag

I have the following file

Hello
World
my
name
is
FalcoGer

And I wish to concatenate the strings of each line.

I wrote the following script to do just that.

#! /usr/bin/bash
myFile=/home/FalcoGer/testfile.txt

result=""
cat $myFile | while read line
do
  result+="$line "
done

echo Result: $result

However I only get Result: with an empty string. When I print it from within the loop it seems to work just fine. What's wrong with this script and how do I fix it?

uz flag
Jos
You can't concatenate strings like that in bash. Try `result="$result $line"` .
hr flag
See [Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?](https://unix.stackexchange.com/questions/9954/why-is-my-variable-local-in-one-while-read-loop-but-not-in-another-seemingly)
FalcoGer avatar
mx flag
@Jos yes you can. `+=` is a valid operator in bash. https://stackoverflow.com/questions/4181703/how-to-concatenate-string-variables-in-bash
waltinator avatar
it flag
Always paste your script into `https://shellcheck.net`, a syntax checker, or install `shellcheck` locally. Make using `shellcheck` part of your development process. Also, the blank on the first line is wrong. Use "`#!/bin/bash`".
uz flag
Jos
Today I learned :-)
hr flag
BTW I assume you're just using this as an exercise to learn about loops - if not, there are better options such as `result=$(paste -sd ' ' < myFile)` or (if you want to use shell constructs only, perhaps `readarray -t arr < myFile; result=${arr[*]}`
FalcoGer avatar
mx flag
@steeldriver actually i use this to download workshop content from steamcmd using a text file. I just broke it down for the minimum example. I prefer to have readable syntax over short one. I need to process the `$line` in any case, so I need a loop.
hr flag
If you need to process the lines, that's usually even more of a reason **not** to use a shell loop. See for example [Why is using a shell loop to process text considered bad practice?](https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice)
Score:4
mx flag

Using the pipe essentially creates a new script with a new scope. You can avoid the pipe like so:

#! /usr/bin/bash
myFile=/home/FalcoGer/testfile.txt

result=""
while read -r line
do
  result+="$line "
done < $myFile

echo Result: $result
cn flag
This is [BashFAQ 24](http://mywiki.wooledge.org/BashFAQ/024)
bac0n avatar
cn flag
You usually want to add `-r` to read.
FalcoGer avatar
mx flag
@bac0n it would appear so.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.