Answer a question

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

Answers

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.

Logo

更多推荐