Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display output of a Bash command and keeping the output in a variable

I'm not sure if it is possible but what I want to do is to run a bash command and storing the output in a variable AND display it as if I launched the command normally. Here is my code:

VAR=`svn checkout $URL`

So I want to store the output in VAR and still see the result (and because svn checkout takes a long time, I can't do echo $VAR just after..)

Thanks

like image 646
Selmak Avatar asked Jan 17 '10 17:01

Selmak


People also ask

How do I display a variable in bash?

Variables are an essential feature of bash programming in which we assign a label or name to refer to other quantities: such as an arithmetic command or a value. They are used to make the machine programs more readable for humans. Using the echo command you can display the output of a variable or line of text.

How do you echo the output of a command?

The echo command writes text to standard output (stdout). The syntax of using the echo command is pretty straightforward: echo [OPTIONS] STRING... Some common usages of the echo command are piping shell variable to other commands, writing text to stdout in a shell script, and redirecting text to a file.


2 Answers

If the command is run from a terminal, you can do:

VAR=$(svn checkout $URL | tee /dev/tty)
like image 92
Alok Singhal Avatar answered Sep 30 '22 16:09

Alok Singhal


You don't have to call the external tee:

VAR=$(svn checkout $URL) && echo $VAR

or even:

VAR=$(svn checkout $URL); echo $VAR
like image 42
Dennis Williamson Avatar answered Sep 30 '22 16:09

Dennis Williamson