How do I store the output of a git command in a variable?
You can use:
var=$(git status 2>&1)
i.e. redirect stderr to stdout and then capture the output.
Otherwise when for error messages are written on stderr
and your command: var=$(git status)
is only capturing stdout
.
How to get output of git commands into Python variables
You can use:
GitPython
Or 'native way':
from subprocess import Popen, PIPE
cmd = 'git diff --name-only'
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
files_changed_list = stdout.decode("utf-8").splitlines()
bash add git command result to local variable
To assign the output of a command to a variable, you need to use command-substitution syntax in bash
.
local currentBranch="$(git rev-parse --abbrev-ref HEAD)"
local gitNumberOfCommits="$(git rev-list --count HEAD)"
What you have done is stored the literal string in those local variables defined. When shell was tokenizing them to evaluate it has understood it as a variable assignment gone horribly wrong! What it understood is a local
variable in the name of currentBranch
set to a value git
and tries to run rev-list
as a command which it cannot obviously find in its list of standard paths (under $PATH
)
How do I display git output in bash and store one string from the output in a variable?
One solution is to use tee
; just not exactly the way you showed. Taking it step by step will perhaps make the solution easier to understand:
git push "$target" --all
will send the error you want to STDERR. That's why you added 2>&1
, to redirect STDERR to STDOUT.
git push "$target" --all 2>&1
Then your pipeline (grep, etc.) is able to pick it up and eventually the variable capture is able to see it when you do
RESPONSE=$(git push "$target" --all 2>&1 | grep "error:" || true)
But because the error is no longer going to STDERR, and STDOUT is now being captured instead of sent to the screen, the output disappears.
So what you want to use tee
for, is to put the output on both STDERR (for the screen) and STDOUT (for your pipeline and eventual variable capture).
RESPONSE=$(git push "$target" --all 2>&1 |tee >(cat 1>&2) | grep "error:" || true)
This will likely work as you intend, but be aware that everything you see on the screen - all output from the git
command, error or otherwise - is now being passed on STDERR.
There aren't many practical reasons why this would be better than the answer about capturing to the variable and then echoing the variable (per miimote's answer), but if for some reason the non-sequential command structure seems better to you, this is a way to do it.
Save git output to a variable
You put a space after the equals sign.
content= $(git diff --cached $line)
^
there
That space means Bash sets the environment variable content
to the empty string for the command specified by the rest of the line, instead of setting the shell variable content
to the result of running the command.
Related Topics
Bash: Read Stdin from File and Write Stdout to File
What Is the Safest Way to Run an Executable on Linux
Debugging Shared Libraries with Gdbserver
Getting "I Won't Open a Connection To" When Connecting to Ftp Server from Google Compute Engine
Linux: How to Detect That Ftp File Upload Is Finished
Filtering Rows Based on Number of Columns with Awk
How to Remove Only the First Occurrence of a Line in a File Using Sed
Perf Stat Does Not Count Memory-Loads But Counts Memory-Stores
Pycharm Startup Error: Unable to Detect Graphics Environment
PDF Lib Install Fail on Linux Server. Using Pecl Install PDFlib
Npm Can't Install Appjs. Error: Cannot Find Module 'Graceful-Fs'
How to Change the Host Type for a 'Canadian Cross' Compilation of Gcc with Crosstool-Ng
Timestamp Accuracy on Ext4 (Sub Millsecond)
Why File Is Accessible After Deleting in Unix
Use Crontab Job Send Mail, the Email Text Turns to an Attached File Which Named Att00001.Bin
What's the Practical Limit on the Size of Single Packet Transmitted Over Domain Socket