Reputation: 2707
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?
Upvotes: 2
Views: 2004
Reputation: 15607
Alternatively:
import os
def my_task():
with lcd(os.environ['TEST_PATH']):
local('pwd')
os.getenv('TEST_PATH')
may also be used (with a default, optionally)
Hat tip: Send bash environment variable back to python fabric
Upvotes: 0
Reputation: 2707
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.
Upvotes: 4
Reputation: 42500
You can capture the value by using echo
testdir = str(run("echo $TEST"))
with cd(testdir):
run("program")
Upvotes: 2