1

I make a shell script that can update a tools. I want that when i run the

root@host:# sh script.sh

then the output will be

Its installing
Its done

But now my script look like

root@host:# sh script.sh
Its installing
Showing internat script working outputs
Its done

As i know stty -echo command hide stdin in terminal and stty echo enable stdin show in terminal. But how can i hide the stander output by shell script ?

7
  • Unwanted output can be redirected to /dev/null, or to a file in case you need it for subsequent diagnostics.
    – AFH
    Commented May 10, 2015 at 13:27
  • tldp.org/guides.html -> Two guides about bash-programming. These will answer all your questions.
    – Hannu
    Commented May 10, 2015 at 13:45
  • @AFH. I try to redirect /dev/null but i failed.
    – Mastan
    Commented May 10, 2015 at 13:58
  • check if you have at first line something like: #!/bin/bash -x and if yes remove -x Commented May 10, 2015 at 14:00
  • 1
    You don't use the pipe symbol with redirection: your command should be mv /root/Desktop/lynis/plugins/* /etc/lynis/plugins/ &>/dev/null. The pipe symbol sends the output to another program's input; redirection sends the output to a file: you can't do both (although that the program to which output is piped can have its own output redirected, as in prog1 | prog2 &> file3).
    – AFH
    Commented May 12, 2015 at 0:31

1 Answer 1

1

Redirect both stdout and stderr to /dev/null:

> /dev/null 2>&1

OR in bash:

&> /dev/null

You can do it for all programs spawned by your script by using exec with the redirection at the beginning of your script.

exec > /dev/null 2>&1

Unless the programs you invoke in your script access the terminal directly (rare), this should cover you.

One note on redirections: The order matters. It needs to be > /dev/null 2>&1, NOT 2>&1 >/dev/null. I used to think the latter would work because > looks like an arrow which makes me think of pointers (as does the word "redirect"), and if I point stderr to stdout and then point stdout to /dev/null, then both should be pointing to /dev/null. That is not the case, though. File descriptors are not pointers and it is more helpful to think of > as sort of an assignment to a file descriptor rather than pointing. (Sort of, because technically, a fd is just a number and you need system functions like dup2 to open a different file into the same file descriptor; but I think assignment is a good high-level abstraction).

You must log in to answer this question.

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