尝试在 ssh heredoc 中进行 cd

Trying to cd in ssh heredoc

我正在编写一个启动ssh会话然后在远程服务器上执行屏幕截图的脚本,脚本如下。

numbah=""
. . . . 
# Taking the screenshot
ssh -x ${INPUT_USER}@nano-machine /bin/bash <<- EOF
  cd /tmp
  mkdir --parents -- temp_img && cd -- $_
  DISPLAY=:0 scrot "Screenshot-${numbah}_%d-%m-%Y.png"
  echo "first arg is   "
EOF

代码块似乎产生错误,导致 scrot 失败。

/bin/bash: line 2: cd: /bin/bash: Not a directory

但是如果我引用 EOF,将不会有任何错误——除了变量不会扩展,这对这个脚本至关重要。

如何在保持变量 intact/expandable 的同时消除错误?

How to get rid of the error whilst keeping the variable intact/expandable ?

禁止扩展这个特定变量。记住引号。

ssh -x ${INPUT_USER}@nano-machine /bin/bash <<- EOF
  cd /tmp
  mkdir --parents -- temp_img && cd -- "$_"
  DISPLAY=:0 scrot "Screenshot-${numbah}_%d-%m-%Y.png"
  echo "first arg is   "
EOF

但我真的发现对于复杂的脚本来说,将数据作为参数传递更容易,并且脚本根本不展开:

ssh -x ${INPUT_USER}@nano-machine /bin/bash -- "$numbah" <<-'EOF'
  cd /tmp
  mkdir --parents -- temp_img && cd -- "$_"
  DISPLAY=:0 scrot "Screenshot-_%d-%m-%Y.png"
  echo "first arg is   "
EOF