TristanP TristanP - 5 months ago 22
Bash Question

Problems with fish shell and ssh remote commands

I use fish shell on my desktop.

We use many servers running nginx within docker. I've tried to create a function so I can ssh to the servers and then log into the docker.

The problem is fish is complaining about the $ in the command, but the command is the one to be executed on the remote server (running bash), not on my machine running fish. I've simplified the script to make it easier to see.

config.fish snippet

function ssh-docker-nginx
ssh -t sysadmin@10.10.10.10 "sudo bash && docker exec -it $(docker ps | grep -i nginx | awk '{print $1}') bash"
end


Fish error:

$(...) is not supported. In fish, please use '(docker)'.
~/.config/fish/config.fish (line 59): ssh -t sysadmin@10.10.10.10 "sudo bash && docker exec -it $(docker ps | grep -i nginx | awk '{print $1}') bash"
^
from sourcing file ~/.config/fish/config.fish
called during startup


Is there a way to get fish to ignore this?

Answer

You'll want to single-quote that argument.

In double-quotes (") fish will try to expand everything that starts with a $, so it will see that $( and then print the error for it. But it will also see the $1 in your arguments to awk and expand that.

And when you want single-quotes to go to the called command (like here, where you want the argument to awk to be single-quoted because this'll go through bash's expansion), you need to escape the quotes with \.

Try

ssh -t sysadmin@10.10.10.10 'sudo bash && docker exec -it $(docker ps | grep -i nginx | awk \'{print $1}\') bash'
Comments