1

I'm struggling a bit with this task, for example I can do a simple loop to send variables values into a file with for loop. But how to get something before output of the variable value? I did something like this as a workaround:

list="$1\n$2\n$3"
varNames=$'var1=\nvar2=\nvar3='


for i in $varNames $list; do
    echo -e $varNames$list > "vars-$(date +%Y-%m-%d_%H.%M).file"
done

But I cant get the loop correct to iterate through out every item in both lists,but the output I'm getting is:

var1= var2= var3=$1
$2
$3

As above $1,$2,$3 are correct values of variables that I created before. What I'm missing in this loop? Is it good way to create two lists or it can handled by some sed afterwards?

Thank you!

  • 1
    Welcome to the site. Could you check the example code you showed us? For example, you loop using a variable i but you never use it in the actual loop. Currently, you would simply execute your echo statement as many times as there are elements in $varNames and $list – AdminBee May 11 at 9:28
  • Sorry I made an error while editing. Now the output is right. What i want to accomplish is to have var1=$1 and so on in new lines respectively. ``` – Dawid May 11 at 9:31
  • Thank you, I'll check out how goes with array keys and I hope it will work out! – Dawid May 11 at 9:44
2

There are almost as many issues as you have lines in your script, I try to go through one by one, at each step assuming everything else is working as intended.

1.

list="$1\n$2\n$3"

This won't work, because the \n will not be replaced by newline, you would need something like this:

list="$1"$'\n'"$2"$'\n'"$3"

or

list="$1
$2
$3"

2.

for i in $varNames $list; do

Assuming you fixed (1), this loop will run 6 times (Disclaimer, see last sentence of this). One time for each of $varNames and $list, which I don't think is intended. Also, when you loop like this, the variables will not be split by newline, but by $IFS. It defaults to newline, space or tab. So it works, but knowing that, you could also just use list="$1 $2 $3" in (1). This only works of course, if you know each of these variables don't contain any of newline, space or tab.

3.

echo -e $varNames$list

outputs the whole variables and not just the split up parts which would be $i.

4.

You overwrite the whole file for each iteration. Better use

for ...; do ... done > file

So what now?

I think you cannot easily fix your code as is. Instead, you should use bash arrays and iterate over the keys:

#!/bin/bash

list=("$1" "$2" "$3")
varNames=("var1" "var2" "var3")

for k in "${!varNames[@]}"; do
    printf '%s=%s\n' "${varNames[$k]}" "${list[$k]}"
done > "vars-$(date +%Y-%m-%d_%H.%M).file"

I prefer using printf (see this), but you could also use echo like this instead if you don't care ...

echo "${varNames[$k]}=${list[$k]}"
| improve this answer | |
  • +1 Thank you very much for your great review. Solution that you provided works perfect. – Dawid May 11 at 10:21
  • @Cbhihe Thank you, newbie mistake. Cheers! – Dawid May 11 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.