I need to construct redis dummy data, and when constructing redis hash
data, I want to construct hash data for a large number of fields
through a for loop.
I call the printf construct directly to achieve the desired result, but when stored in a variable, the result is not as expected, the sample code is as follows
#!/bin/bash
for no in $(seq 5); do
printf "%s%s %s " "$data" "filed$no" "val$no"
done
# stdout:
# bash test.sh
filed1 val1 filed2 val2 filed3 val3 filed4 val4 filed5 val5
save to variable
#!/bin/bash
data=""
for no in $(seq 5); do
data="$(printf "%s%s %s " "$data" "filed$no" "val$no")"
done
printf "%s\n" $data
# stdout
# bash test.sh
filed1
val1
filed2
val2
filed3
val3
filed4
val4
filed5
val5
This seems to be escaping spaces into line breaks, how can I solve it? I really appreciate any help with this.