Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have mulitiple ip adresses in a variable.


I want to save them to a file like this

how do i do this?

share|improve this question
up vote 3 down vote accepted

You can do it like this.

rm -f file
for i in ${ActiveIPs}
  echo ${i} >> file
share|improve this answer


printf "%s\n" $ActiveIPs > ActiveIPs.txt

This behaves as follows:

  • $ActiveIPs is expanded by the shell.
    • word splitting breaks it at whitespace characters. The pieces are passed to printf as individual parameters.
    • If it were quoted as in printf "%s\n" "$ActiveIPs", it would not be split, and it would be passed to printf as a single parameter.
  • printf takes the first argument and formats it with "%s\n".
  • If printf is given more parameters than format specifiers, it reuses the format string. In this case, it has 1 format specifier (%s), so it applies "%s\n" to the remaining parameters, one by one.
share|improve this answer
Why the downvote? – ignis Apr 22 '13 at 17:28
I tried your command and it didn't work, it simply printed all IPs in one line. But that was a shell issue. Mea culpa, I can convert -1 to +1 only if you edit your answer (e.g. explicitly include redirection to a file, as asked by OP). – mpy Apr 22 '13 at 22:40
This is not a shell issue: the IPs will be printed on one line. – eldering Apr 23 '13 at 9:47
@eldering Did you quote $ActiveIPs? See edited answer. – ignis Apr 23 '13 at 12:08
@eldering: The problem I faced was that zsh doesn't do word splitting like bash unless the SH_WORD_SPLIToption is set. This is easily forgotten and I didn't knew that the shell's printf can take more arguments than format specifiers (IMHO unlike C's printf?!). Now with the additional explanation, I really think that this an elegant answer. – mpy Apr 23 '13 at 12:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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