"Escaping" $ when executing a remote bash command from python fabric

bash, fabric, python

Solution

use echo with single quotes. That will prevent the shell from expanding `$arch`.

run('echo \'Server = http://repo.archlinux.fr/$arch\' | sudo -s tee -a /etc/pacman.conf')

this should be equivalent to

echo 'Server = http://repo.archlinux.fr/$arch' | sudo -s tee -a /etc/pacman.conf

quick testing:

>>> import os
>>> os.system('echo \'Server = /foo/$arch\' ')
Server = /foo/$arch
0

Problem

So I am trying to automate the set up of an arch linux instance via a python fabric script like this: ``` from fabric.api import run, sudo def server_setup_communityrepo(): run('echo \'echo "[archlinuxfr]" >> /etc/pacman.conf\' | sudo -s') run('echo \'echo "Server = http://repo.archlinux.fr/$arch" >> /etc/pacman.conf\' | sudo -s') run('echo \'echo " " >> /etc/pacman.conf\' | sudo -s') sudo('pacman -Syy yaourt --noconfirm') ``` The problem occurs on the second `run()` call because of the $ sign in the `$arch`. This fabric function fails in line 2 because $ followed by a string is recognized by fabric as a config variable. But I actually want $arch to be understood as a literal in the `echo 'echo "Server = http://repo.archlinux.fr/$arch" >> /etc/pacman.conf'` call in bash shell. How do I "escape" from this fabric quirk and designate the $arch as a literal to be written into my pacman.conf file?

Original source