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
  • 252,262
  • 96
  • 511
  • 570