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++))
    echo "mytext$I, " >> myfile.txt   ????? 

I want something like:

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

but instead I get:

  • Please provide sample input and output: "a continuous text at the end of the process" is so ambiguous to be meaningless... – jasonwryan Aug 27 '15 at 8:24
  • I edited my question, hope it is more clear now... – H'H Aug 27 '15 at 8:30
  • echo outputs a newline each pass of the loop. do want echo -n ? – Skaperen Aug 27 '15 at 8:56

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' "$(
while [ "$n" -le 71 ]; do
  printf 'mytext%s\n\n' "$n"
)" | paste -sd', ' - >out
| improve this answer | |
  • so where is myfile.txt in this way? – H'H Aug 27 '15 at 9:01
  • @H'H: Added it, replace out with whatever name you want. – cuonglm Aug 27 '15 at 9:03

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.

| improve this answer | |
  • You will get the laste element as mytext71, . – cuonglm Aug 27 '15 at 8:48
  • I mentioned I want something continuous like this: mytext0, mytext1, mytext2, mytext3, ...., mytext71 – H'H Aug 27 '15 at 9:00

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.