I want to capture a command execution output like below:
$ o=$( git clone [email protected]:my-repo/demo.git )
Cloning into 'demo'...
remote: Counting objects: 176, done.
remote: Compressing objects: 100% (94/94), done.
remote: Total 176 (delta 84), reused 162 (delta 77)
Receiving objects: 100% (176/176), 32.98 KiB | 2.75 MiB/s, done.
Resolving deltas: 100% (84/84), done.
$ echo "$o"
# Empty.
OK, just call me stupid, I haven't redirected the output. So:
$ o=$( git clone [email protected]:my-repo/demo.git 2>&1 )
Printed nothing in my console so I started being optimistic and continued to:
$ echo "$o"
Cloning into 'demo'...
And this is all. The other many lines printed by git
were simply not stored into my humble o
variable.
Then I tried to run the command in a subshell then redirect:
$ o=$( (git clone [email protected]:my-repo/demo.git) 2>&1 )
$ echo "$o"
Cloning into 'demo'...
What is it going on here?
Note: I tried to do my homework but posts like How do I store the output of a git command in a variable? just tell me exactly the first thing I've tried and failed. Many other links just repeat the same.
$ o=$( git clone [email protected]:my-repo/demo.git </dev/null 2>&1 | cat )
make any difference? ( That is an attempt to make git NOT have a console/terminal )--progress
solves the problem. I'll ask him to post an answer. Thanks!