简体   繁体   中英

Unexpected EOF in .bashrc alias

I am getting an unexpected end of file error after including this line in my .bashrc

alias domsrv01='echo -e ?HT_R8\'% | xclip ; ssh 10.50.0.35'

The desired exit from echo to xclip is:

?HT_R8'%

As you may guess, it's a password and I can't change it so:

How can I escape the single quote character from inside the password to fix the EOF error?

Also, I'm not sure if the single quote is the only issue here, can "?" and "%" be interpreted in funny ways too?

You cannot directly escape singe quotes within single quotes in bash. However, since bash concatenates adjacent strings, you can use this construct instead 'text'"'"'moretest' . You actually end the single quoted string with a single quote, and immediately add a double quoted single quote, followed by the remaining of the string (in single quotes). In your specific example, the command would look like this:

alias domsrv01='echo -e ?HT_R8\'"'"'% | xclip ; ssh 10.50.0.35'

More discussion on the topic can be fount here: How to escape single-quotes within single-quoted strings?

Edited: Added the missing backlash noticed by @GordonDavisson

Aliases are intended for short text replacements, not full blown shell commands. Use a function:

domsrv01 () {
    echo -e ?HT_R8\'% | xclip ; ssh 10.50.0.35
}

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