1

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?

sergei
  • 73
  • 1
  • 1
  • 7

2 Answers2

1

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

Community
  • 1
  • 1
user000001
  • 32,226
  • 12
  • 81
  • 108
  • or `'text'\''moretest'` (same as `alias` command, with no parameter outputs.) – anishsane Jul 12 '13 at 10:36
  • 1
    For some reason this still fails. Try this: alias test1='echo -e ?HT_R8'"'"'% | xclip | xclip -o'. It should return the echo output to xclip but it doesn't. – sergei Jul 12 '13 at 10:49
  • @sergei You are right, it doesn't... a workaround might be to use a function like `test2() { echo -e '?HT_R8'"'"'%' | xclip | xclip -o; }` . (unalias the existing alias first if you use the same name) – user000001 Jul 12 '13 at 11:47
  • 1
    It's missing the baskslash in the alias itself, so when you use the alias it results in an unterminated single-quoted string. Try: `alias domsrv01='echo -e ?HT_R8\'"'"'% | xclip ; ssh 10.50.0.35'` – Gordon Davisson Jul 12 '13 at 14:48
1

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
}
chepner
  • 497,756
  • 71
  • 530
  • 681
  • This aliases file started as a lazy replacement for a connection manager for more than 100 servers. I may need to move to functions sometime in the future, so I'll keep that in mind. – sergei Jul 12 '13 at 15:17