1

The command output gets stored in a variable. Currently my script outputs it all on one line, instead of keeping it seperated on multiple lines. Which is not what I want, because this way it looks unorganized and is hard to read. How can I store the command output in my variable, so that it will get printed out on multiple lines and not just one?

#! /bin/bash

#User input
echo -n 'Enter IP: '
read IP

#Scanning the Ports
ports=$( nmap -sS $IP | grep open)

#Output
echo $ports

Example:

What it currently outputs

21/tcp open ftp 22/tcp open ssh 23/tcp open telnet

What I would want it to output

21/tcp open ftp 
22/tcp open ssh 
23/tcp open telnet

1 Answer 1

4

You need to use quotes.

echo "$ports"

When bash sees the line without quotes, it performs word splitting. In other words, it's as if you executed:

echo 21/tcp open ftp\
22/tcp open ssh\
23/tcp open telnet

Which treats the newlines no differently that the spaces, and passes each argument to echo. It then writes each argument, separated by a single space.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.