1

I am passing a text file to a bash while running. Text file has contents I want to supply to a java program as argument. Text file has each content in a new line. The contents print fine within the loop but I need to create a concatenated string with all the contents to pass to java program and appending to a string variable in loop is not working. This is how the program looks like:

#!/bin/bash
args=""
for var in $(cat payments.txt)
do 
  echo "Line:$var"
  args+="$var "
done
echo "$args"

It prints:

Line: str1
Line:str2
 str2  // args should have appended values of each line but it got only last line

File looks like:

str1
str2

Can anyone suggests what I am doing wrong here?

Thanks

7
  • 1
    you might want to try args=$(echo $(<payments.txt)) Commented Feb 3, 2022 at 10:07
  • 1
    @Fravadona Very nice. Note that echo is however a bit risky when the input is not known. Try with a file that starts with -e \c, for instance. Commented Feb 3, 2022 at 10:19
  • @RenaudPacalet you're right, it all depends on the content of payments (numbers?). Given how OP is concatenating args (adding a space at the end), echo can be replaced with printf '%s ' Commented Feb 3, 2022 at 10:25
  • @Fravadona Yes, I'd personally prefer printf. I don't like echo. Commented Feb 3, 2022 at 10:32
  • for line in $(cat file) ... doesn't work in general. See Bash Pitfalls #1 (for f in $(ls *.mp3)). Shellcheck identifies this problem. Commented Feb 3, 2022 at 11:12

2 Answers 2

1

Edit: the issue was due to \r\n line endings.

for var in $(cat payments.txt) is a nice example of useless use of cat. Prefer a while loop:

#!/bin/bash
args=""
while IFS= read -r var; do
  args+="$var "
done < payments.txt
echo "$args"

But instead of looping, which is not very efficient with bash, you could as well use a bash array:

$ declare -a args=($(< payments.txt))
$ echo "${args[@]}"
str1 str2

"${args[@]}" expands as separate words. Use "${args[*]}" to expand as a single word . If your line endings are \r\n (Windows) instead of \n (recent macOS, GNU/Linux), the \r will interfere. To remove the \r before printing:

$ echo "${args[@]%$'\r'}"
Sign up to request clarification or add additional context in comments.

14 Comments

I have the same output as the original one having only the last value. #!/bin/bash while IFS= read -r line; do args="$args $line" done < payments.txt echo "$args"
Strange. I cannot reproduce this here. With a file containing two lines (str1 and str2) I get str1 str2. Are you sure you typed exactly this?
Suggested lines in above comment are doing the same thing, printing only one value!
There must be something wrong with your input file or the way you use this. If it really contains several lines it is impossible that declare -a args=($(< payments.txt)); echo "${args[@]}" prints only one. My guess: you put all this in a script file, execute it and type echo "$args" yourself on the command line. If you do so it is not the same args variable that you use: the args variable of the script disappeared and the one you echo comes from the parent shell.
Do not put all this in a script file. Try to type the two last proposed commands yourself on the command line and tell us what you see. We'll package all this in a script after it has been fully tested and validated on the command line.
|
0

Your first echo is printing out the combination and not storing it in a new variable. Try:

#!/bin/bash
args=""
for var in $(cat payments.txt)
do 
  echo = "Line:$var" # this line prints but doesn't alter $var
  args+="Line:$var2 " #add Line: in here
done
echo "$args"

1 Comment

I don't need to append text "Line:" while appending. That was just used in echo to see if I am getting each line value right. I need args variable which should have each of these variable concatenated having space in between. Purpose is to prepare this to pass to java program as argument. Can you please let me know what is wrong in original line to prepare args and why does it not have variables concatenated?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.