1

When I run this bash script below my results are all echoed on the same line, as "Example Output" below shows.

#!/bin/bash
Commander=$(nmap -vv -p 8080 xxx.149.xxx.100-xxx | 
            grep "Discovered open port 8080/tcp     on" | sed -r 's/^.{32}//');
echo $Commander;

Example Output:

xx.149.xx.115 xx.149.xx.107 xx.149.xx.107 xx.149.xx.101 xx.149.xx.118 xx.149.xx.146

What I would like is:

xx.149.xx.115 
xx.149.xx.107 
xx.149.xx.107 
xx.149.xx.101 
xx.149.xx.118 
xx.149.xx.146 
xx.149.xx.19

Any, Suggestions??

Philcinbox
  • 77
  • 2
  • 11

1 Answers1

4

Just quote your variable when calling it:

echo "$Commander"

Sample

$ myvar="hello
> i am fedor
> fedorqui"

$ echo $myvar
hello i am fedor fedorqui

$ echo "$myvar"
hello
i am fedor
fedorqui

So you have space separated output and you want it to be new line separated? If so, pipe to tr ' ' '\n', that will do this replacement.

$ d="a b c"
$ echo "$d" | tr ' ' '\n'
a
b
c
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • I cant exactly go this route because of how nmap is piped to grep. What I thought would have to happen is insert a "\n" in the sed command but at this point not exactly sure?? – Philcinbox Jan 20 '14 at 16:13
  • 1
    Aaaah I think I misunderstood your question. So you have space separated output and you want it to be new line separated? If so, pipe to `tr ' ' '\n'`, that will do this replacement. – fedorqui Jan 20 '14 at 16:14
  • What I think I'm going to try is getting rid of sed and just use tr, I'll let you know how it goes. – Philcinbox Jan 20 '14 at 16:25
  • 1
    echo $Commander | tr ' ' '\n'; – Philcinbox Jan 20 '14 at 16:40
  • Nice to read that, @Philcinbox : ) – fedorqui Jan 20 '14 at 16:41