I am writing bash script and inside I am executing a command. I want to save the output of the command to variable but also want to print the output of the command to the standard output. I dont want to print the variable once the command is completed. How can I achieve this ?
Asked
Active
Viewed 2,275 times
1
-
1Check `man tee` – anubhava May 14 '19 at 19:15
-
Note that questions about *using* UNIX tools belong on [unix.se] or [SuperUser](https://superuser.com/), not Stack Overflow. Our scope is specific to *developing* software. – Charles Duffy May 14 '19 at 19:30
-
1This is also a duplicate of [How to store the output of a command in a variable at the same time as printing?](https://stackoverflow.com/questions/37067895/how-to-store-the-output-of-a-command-in-a-variable-at-the-same-time-as-printing/37067964) – Charles Duffy May 14 '19 at 19:31
-
With Linux: `var=$(your_command | tee >(cat - >&255))` – Cyrus May 14 '19 at 20:00
-
@CharlesDuffy Any reason you're not closing as duplicate? – Benjamin W. May 14 '19 at 20:11
-
@BenjaminW Already voted-to-close with a different reason so I can't. – Charles Duffy May 14 '19 at 20:34
1 Answers
1
Try the following, it will print the output of your command and assign to variable.
VAR="$(your_command| tee /dev/tty)"

Ardit
- 1,522
- 17
- 23
-
4That's not printing *to standard output*, though, it's printing to the TTY. – Charles Duffy May 14 '19 at 19:30