I'm trying to build a dynamic command to execute in an .sh file, however, when I do it, it seems that somehow it adds \r characters so command is not correctly executed.

Notice that I'm running this over windows, using bash.exe. A sample of code:

My command.cmd file:

bash --login -x command.sh Parameter1

My .sh file:

#export PATH=.
cmd="ssh-agent bash -c 'ssh-add /cygdrive/C/$1/id_rsa & lftp -u myUser, -e \"mirror --verbose / /cygdrive/C$1/Output; bye\" sftp://domain.com -p 22'"
eval "$cmd"

If I run the command statically, it works perfectly:

ssh-agent bash -c 'ssh-add /cygdrive/C/$1/id_rsa & lftp -u myUser, -e \"mirror --verbose / /cygdrive/C$1/Output; bye\" sftp://domain.com -p 22'

But if I run it as is on the .sh file, it outputs the following:


C:\MyFolder\bin>bash --login -x command.sh Parameter1
+ cmd='ssh-agent bash -c '\''ssh-add /cygdrive/C/Parameter1/id_rsa & lftp -
u myUser, -e "mirror --verbose / /cygdrive/C/Parameter1/Output; bye" sft
'://sftp.domain.com -p 22'\''
+ eval 'ssh-agent bash -c '\''ssh-add /cygdrive/C/Parameter1/id_rsa & lftp
-u myUser, -e "mirror --verbose / /cygdrive/C/Parameter1/Output; bye" sf
'p://sftp.domain.com -p 22'\''
++ ssh-agent bash -c 'ssh-add /cygdrive/C/Parameter1/id_rsa & lftp -u myUser, -e "mirror --verbose / /cygdrive/Parameter1/Output; bye" sftp://sft
'.domain.com -p 22
Identity added: /cygdrive/id_rsa (rsa w/o comment)
'] `/' [Bad port '22.]

My guess is since this is running on windows something is not well formed in the dynamic command variable so the command does not run well. If instead of eval I perform echo, the output is:


C:\MyFolder\bin>bash --login -i command.sh Parameter1
ssh-agent bash -c 'ssh-add /cygdrive/C/Parameter1/id_rsa & lftp -u myUser, -e "mirror --verbose / /cygdrive/C/Parameter1/Output; bye" sftp://sftp.domain.com -p 22'

Which seems perfect.

What am I not doing right here?

Thank you.

  • Try trimming your input (remove leading/trailing whitespace) – Nifle Dec 12 '14 at 20:38

Nevermind, found out that it was Windows adding invalid characters. With Notepad++, saving as Unix/Linux worked like a charm.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.