如何将长字符串(带有新行)变量作为环境传递给ssh命令

KEY=$(cat ~/.ssh/id_rsa.pub)
ssh root@127.0.0.1 KEY="$KEY" echo "hi"

它将失败并显示如下错误:

bash: AAAAB3NzaC1yc2EAAAADAQABAAABAQDJ65FHdb8FyD...tpFnRUSZ: No such file or directory

我该如何解决?

qq_zjy3 回答:如何将长字符串(带有新行)变量作为环境传递给ssh命令

假设使用了bash:

对于更一般的情况,如果输入中可能包含任意字符,包括引号,请考虑使用(特定于bas的)${var@A}创建分配:

ssh root@localhost ${KEY@A} echo 'hi'

更新1:bash(4.4版)手册页:在“ @A”(和其他操作)上:

  ${parameter@operator}
          Parameter  transformation.   The  expansion  is either a transformation of the value of parameter or information
          about parameter itself,depending on the value of operator.  Each operator is a single letter:

          Q      The expansion is a string that is the value of parameter quoted in a format that can be reused as input.
          E      The expansion is a string that is the value of parameter with backslash escape sequences expanded as with
                 the $'...' quoting mechansim.
          P      The  expansion  is a string that is the result of expanding the value of parameter as if it were a prompt
                 string (see PROMPTING below).
          A      The expansion is a string in the form of an assignment statement or declare command that,if  evaluated,will recreate parameter with its attributes and value.
          a      The expansion is a string consisting of flag values representing parameter's attributes.
本文链接:https://www.f2er.com/3145511.html

大家都在问