Execute a command over ssh running over ssh

  • 5th Jun 2025
  • 1 min read

I was at my job, and I needed to wanted to execute this command:

curl -H "Authorization: Bearer $TOKEN" -O "$URL"
command1
command2

But there was a twist: this command needed to be executed over ssh, executed over ssh. I did a few research and I was surprised to realize how little people had this issue and how much chatgpt hallucinated over this task. After reading a bit documentation here is the easiest solution I found

ssh host1 ssh host2 <<EOF
curl -H "Authorization: Bearer $TOKEN" -O "$URL"
command1
command2
EOF

With this code $TOKEN and $URL will evaluated on the host running this script. If you want to evaluate variable on the host executing the curl, bash has a special syntax for that:

ssh host1 ssh host2 <<'EOF'
curl -H "Authorization: Bearer $TOKEN" -O "$URL"
command1
command2
EOF

Note the ' around the EOF