简体   繁体   中英

Special character in string variable in shell script

PubkeyContent=$(cat $Pubkey)    
read -r -d '' SSH_SCRIPT <<EOM
    \$(grep ${PubkeyContent} ~/.ssh/authorized_keys")
EOM

I got the wrong result.

[Debug] SSH_SCRIPT = $(grep ssh-rsa AAAAB3NzaC1yc2EAAAzSb7WKnMsysGA8kuq5Ysp02Y75d5 sam ~/.ssh/authorized_keys")com

I want this result

 [Debug] SSH_SCRIPT = $(grep "ssh-rsa AAAAB3NzaC1yc2EAAAzSb7WKnMsysGA8kuq5Ysp02Y75d5 sam@com" ~/.ssh/authorized_keys")

A simple fix would be to quote the PubkeyContent variable content inside the here-doc, ie

read -r -d '' SSH_SCRIPT <<EOM
    \$(grep "${PubkeyContent}" ~/.ssh/authorized_keys")
EOM

or if you are using bash/ksh/zsh shell it supports a way to automatically slurp in the file content using input re-direction < file and thereby avoiding the useless use of cat . You can apply that inside command substitution as

read -r -d '' SSH_SCRIPT <<EOM
    \$(grep "$(<"$Pubkey")" ~/.ssh/authorized_keys")
EOM

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM