2

I want to add text to a file using shell-scripts. The solution I know is if you want to insert your text into a newline (appending):

echo "mytext" >> myfile.txt 

what I want to have a continuous text at the end of process.

for ((I=0; I <72 ; I++))
  do
    echo "mytext$I, " >> myfile.txt   ????? 
done

I want something like:

mytext0, mytext1, mytext2, mytext3, ...., mytext71

but instead I get:

mytext0, 
mytext1, 
....
mytext71
cuonglm
  • 153,898
H'H
  • 123
  • 1
  • 5

2 Answers2

1

You just need to make sure you don't append a newline to the end of the output: just replace echo with either echo -n or printf. I'd recommend the latter as it's more portable.

l0b0
  • 51,350
1

In bash (and also shell which support brace expansion), you can do:

printf '%s\n' "$(printf 'mytext%s\n\n' {1..71})" | paste -sd', ' - >out

In POSIX shell:

printf '%s\n' "$(
n=1
while [ "$n" -le 71 ]; do
  printf 'mytext%s\n\n' "$n"
  n=$((n+1))
done
)" | paste -sd', ' - >out
cuonglm
  • 153,898