Search code examples
pythonbashfabriccd

Using environment variables in Fabric


Assuming:

export TEST=/somewhere

I want to run the command /somewhere/program using:

with cd('$TEST'):
  run('program')

However, this doesn't work because the $ gets escaped.

Is there a way to use an environment variable in a Fabric cd() call?


Solution

  • Following suggestion from @AndrewWalker, here is a more compact solution that worked for me (and to my knowledge, the result is the same):

    with cd(run("echo $TEST")):
      run("program")
    

    But I decided to go for a (very slightly) more concise yet as readable solution:

    run('cd $TEST && program')
    

    This second solution, if I am correct, produces the same result.