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.

wrufesh
  • 1,379
  • 3
  • 18
  • 36
  • 1
    Possible duplicate of [How does "cat << EOF" work in bash?](https://stackoverflow.com/questions/2500436/how-does-cat-eof-work-in-bash) – David C. Rankin Dec 25 '18 at 08:27

1 Answers1

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"
oguz ismail
  • 1
  • 16
  • 47
  • 69