shell function issue - remove the single quote from shell statement
Note: At this point it is not clear what specific shell the OP uses. This answer makes a general point and then discusses bash
and zsh
.
Your immediate problem is unrelated to quoting: The error message suggests that the executable sshpass
is not in your $PATH
.
Your string ($cmdstr
) doesn't actually contain single quotes - they are an artifact of running bash
or zsh
with set -x
in effect.
However, there are additional issues:
The way you parse the alias definition in cmssh()
turns the \\
in your alias definition from a (conceptually) quoted single \
into literal string \\
(two literal \
chars.)
A simple fix in this case is to add another layer of evaluation by having xargs
(without arguments) echo the string:
cmdstr=$(alias $aliascmd | cut -d"'" -f2 | xargs)
Note that I've eliminated the =
-based cut
command, which is not needed.
In general, though, parsing alias definitions this way is fragile.
At that point I would expect your code to work in bash
, though not yet in zsh
(see below).Note that simply removing function
from the beginning of your function definition would make your code work in sh
(a POSIX-features-only shell), too.
The final problem affects only zsh
, not bash
:
zsh
by default doesn't perform word splitting on $cmdstr
in the sshpass -p "$cmpw" $cmdstr
command, so that 'ssh mydomain\userxyz@host.com'
is passed as a SINGLE argument to sshpass
.
To turn on word splitting when evaluating $cmdstr
and thus pass its tokens as separate arguments, refer to it as ${=cmdstr}
:
sshpass -p "$cmpw" ${=cmdstr}
Note that this feature is zsh
-specific.