Golo Roden Golo Roden - 2 months ago 9x
Git Question

Forward exit code in bash if

I want to run a command on bash that shows the output of

git status
, but only if something interesting is there. In other words: If everything is fine, I don't want the command to print anything. Basically I can achieve this by running:

if [[ $(git status -s) ]]; then git status; fi

The trick here is that the
flag only outputs anything if there is something interesting, so it does exactly what I want to :-)

The only drawback is that if I run this in a directory that is not a git directory, I don't get an exit code not equal to
. If I run
git status
git status -s
directly, both fail with exit code
. But as soon as I do this within the
, the exit code is

How can I enhance my script so that the exit code will be forwarded?


Do the exit status check within [[ too:

if [[ -n $(git status -s 2>/dev/null) && $? -eq 0 ]]; then git status; fi