I have a script where it checks whether a package is installed or not and whether the port 8080 is being used by a particular process or not. I am not experienced at all with bash, so I did something like this:

if dpkg -s net-tools; then
    if  netstat -tlpn | grep 8080 | grep java; then
        echo "Shut down server before executing this script"
    echo "If the server is running please shut it down before continuing with the execution of this script"

# the rest of the script...

However when the script is executed I get both the dpkg -s net-tools and the netstat -tlpn | grep 8080 | grep java outputs in the terminal, and I don't want that, how can I hide the output and just stick with the result of the ifs?

Also, is there a more elegant way to do what I'm doing? And is there a more elegant way to know what process is using the port 8080 (not just if it's being used), if any?

3 Answers 3


To hide the output of any command usually the stdout and stderr are redirected to /dev/null.

command > /dev/null 2>&1


1.command > /dev/null: redirects the output of command(stdout) to /dev/null
2.2>&1: redirects stderr to stdout, so errors (if any) also goes to /dev/null


&>/dev/null: redirects both stdout and stderr to /dev/null. one can use it as an alternate of /dev/null 2>&1

Silent grep: grep -q "string" match the string silently or quietly without anything to standard output. It also can be used to hide the output.

In your case, you can use it like,

if dpkg -s net-tools > /dev/null 2>&1; then
    if  netstat -tlpn | grep 8080 | grep java > /dev/null 2>&1; then
    #rest thing
    echo "your message"

Here the if conditions will be checked as it was before but there will not be any output.

Reply to the comment:

netstat -tlpn | grep 8080 | grep java > /dev/null 2>&1: It is redirecting the output raised from grep java after the second pipe. But the message you are getting from netstat -tlpn. The solution is use second if as,

if  [[ `netstat -tlpn | grep 8080 | grep java` ]] &>/dev/null; then
  • 1
    You could also mention grep -q, and the newer bash &> ... as a synonym for > ... 2>&1 May 30, 2014 at 18:23
  • thank you, that enhaced the script. But I still get a message saying that some processes can't be shown since it's not being executed as root. This happens with netstat. Is there any way I could hide that too?
    – dabadaba
    May 30, 2014 at 18:33
  • Regarding "grep -q "string" [...] also can be used to hide the output": while it hides stdout output, it does not hide stderr "output". E.g. (echo "string via stdout"; echo "string via stderr" >&2) | grep -q "string" "leaks" string via stderr on stderr. This may or may not be desirable. To hide stderr output, including those originating from grep itself: ((echo "string via stdout"; echo "string via stderr" >&2) | grep -q "string") 2>/dev/null.
    – Abdull
    Jul 20, 2023 at 10:12

While flushing the output to /dev/null is probably the easiest way, sometimes /dev/null has file permissions set so that non-root cannot flush the output there. So, another non-root way to do this is by

command | grep -m 1 -o "abc" | grep -o "123"

This double-grep setup finds the matching lines with abc in them and since -o is set ONLY abc is printed and only once because of -m 1. Then the output which is either empty or abc is sent to grep to find only the parts of the string which match 123 and since the last command only outputs abc the empty string is returned. Hope that helps!


lsof -i :<portnumnber> should be able to do something along the lines of what you want.

  • sorry I forgot to add the real question to the post, check it agin please because you answer is just for the "secondary" questions
    – dabadaba
    May 30, 2014 at 17:49
  • also is there I way I could extract the process name/PID from the output of that command?
    – dabadaba
    May 30, 2014 at 17:50
  • Another way to do the same thing is fuser -n tcp 8080, whose output may be easier to parse.
    – fkraiem
    May 30, 2014 at 18:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .