Skip to content

Commit d761cef

Browse files
committed
Use export instead of env to apply environment variables.
This means they properly carry to sub-commands/etc. The env is still reset between operation calls as expected, so this is effectively fixes a bug in the expected behaviour.
1 parent 6a6bad7 commit d761cef

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

pyinfra/api/connectors/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def make_unix_command(
235235
'{0}={1}'.format(key, value)
236236
for key, value in six.iteritems(env)
237237
])
238-
command = StringCommand('env', env_string, command)
238+
command = StringCommand('export', env_string, '&&', command)
239239

240240
if chdir:
241241
command = StringCommand('cd', chdir, '&&', command)

tests/test_connectors/test_util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ def test_command_env(self):
103103
'anotherkey': 'anothervalue',
104104
})
105105
assert command.get_raw_value() in [
106-
"sh -c 'env key=value anotherkey=anothervalue uptime'",
107-
"sh -c 'env anotherkey=anothervalue key=value uptime'",
106+
"sh -c 'export key=value anotherkey=anothervalue && uptime'",
107+
"sh -c 'export anotherkey=anothervalue key=value && uptime'",
108108
]
109109

110110
def test_command_chdir(self):
@@ -129,5 +129,5 @@ def test_mixed_command(self):
129129
assert command.get_raw_value() == (
130130
'sudo -H -n -E -u root ' # sudo bit
131131
'su pyinfra -c ' # su bit
132-
"'bash -c '\"'\"'cd /opt/somedir && env key=value echo hi'\"'\"''" # command bit
132+
"'bash -c '\"'\"'cd /opt/somedir && export key=value && echo hi'\"'\"''" # command bit
133133
)

0 commit comments

Comments
 (0)