Pergunta

Here is a simple thing i was working on

 echo "please enter a command"
 read x
 $x
 checkexitstatus()
 {...}

checkexit status is a ifloop created somewhere else just to check exit status

What i want to know is Is there any way that when i run the $x that it wont be displayed on the screen I want to know if it is possible without redirecting the output to a file

Foi útil?

Solução 2

You could use Bash redirection :

  • command 1> /.../path_to_file => to redirect stdout into path_to_file.

command > /.../path_to_file is a shortcut of the previous command.

  • command 2> /.../path_to_file => to redirect stderr into path_to_file

To do both at the same time to the same output: command >/.../path_to_file 2>&1.

2>&1 means redirect 2 (stderr) to 1 (stdout which became path_to_file). You could replace path_to_file by /dev/null if you don't want to retrieve the output of your command.

Otherwise, you could also store the output of a command :

$ var=$(command) # Recent shell like Bash or KSH
$ var=`command` # POSIX compliant

In this example, the output of command will be stored in $var.

Outras dicas

No, it isn't possible.

$x &> /dev/null

If you want to turn off only the echo command and not other commands that send their output to the stdout, as the title suggests, you can possibly (it may break the code) create an alias for echo

alias echo=':'

now echo is an alias for noop. You can unalias it by unalias echo.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top