1

I am trying to create a file remotely via ssh with the command as follows:

ssh $REMOTE_USER@$REMOTE_HOST "
    cat > hooks/post-receive <<EOF
    #!/bin/bash
    git checkout -f
    EOF
    chmod +x hooks/post-receive
"

After it is successfully executed when I check the file with cat repo.git/hooks/post-receive on remote server I see the following result:

#!/bin/bash
git checkout -f
EOF
chmod +x hooks/post-receive

I expect EOF and chmod +x hooks/post-receive not to be present in post-receive file. What can be done to solve this.

1

1 Answer 1

1

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.

...

If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here-documents within shell scripts to be indented in a natural fashion.

So, you need to remove trailing spaces from your here document or substitute them with tabs.

ssh $REMOTE_USER@$REMOTE_HOST "
cat > hooks/post-receive <<EOF
#!/bin/bash
git checkout -f
EOF
chmod +x hooks/post-receive"

# or,

ssh $REMOTE_USER@$REMOTE_HOST "
cat > hooks/post-receive <<-EOF
    #!/bin/bash
    git checkout -f
    EOF
    chmod +x hooks/post-receive"
Sign up to request clarification or add additional context in comments.

4 Comments

or use <<- and then everything must be indented with a tab and the tab will be ignored in the output.
Concerning <<- please note that only leading tab characters are stripped -- not soft tab characters.
@DavidC.Rankin thanks, edited to add info about <<-.
Good deal, that rounds out the completeness of the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.