The shell is expanding the commands on your machine not the remote one.
Quote 'EOF'
ssh user@host<<'EOF'
STATUS="$(git status)"
EOF
From man bash
:
Here Documents
This type of redirection instructs the shell to read input from the
current source until a line containing only delimiter (with no
trailing blanks) is seen. All of the lines read up to that point are
then used as the standard input for a command.
The format of here-documents is:
<<[-]word
here-document
delimiter
No parameter expansion, command substitution, arithmetic expansion,
or pathname expansion is performed on word. If any characters in word
are quoted, the delimiter is the result of quote removal on word, and
the lines in the here-document are not expanded. If word is
unquoted, all lines of the here-document are subjected to parameter
expansion, command substitution, and arithmetic expansion. In the
latter case, the character sequence \ is ignored, and \
must be used to quote the characters \, $, and `.
Edit
Your test is also wrong. You could use grep
to check if the required string is in the output:
ssh user@host <<'EOF'
if git status 2>&1 | grep -q 'Not a git' ; then
echo 'Not a git repository'
fi
EOF
or if you want to retain the status in a variable
ssh user@host <<'EOF'
STATUS=$(git status 2>&1)
if echo $STATUS | grep -q 'Not a git' ; then
echo 'Not a git repository'
fi
EOF
Note that you will have to redirect STDERR
to STDOUT
with 2>&1
Edit 2
If you force bash
you can also
ssh user@host bash <<'EOF'
STATUS=$(git status 2>&1)
SEARCH_STATUS="ot a git"
echo "STATUS=$STATUS"
if [[ "$STATUS" == *$SEARCH_STATUS* ]]; then
echo 'Not a git repository'
fi
EOF
Beware that on macOS and Linux I got different capitalisations of 'Not'/'not'. Therefore the SEARCH_STATUS
is just ot a git
and not Not a git