diff --git a/sh.py b/sh.py index 04fe4ad4..cab0cf9c 100644 --- a/sh.py +++ b/sh.py @@ -1425,7 +1425,10 @@ def __call__(self, *args, **kwargs): pcall_args.pop("with", None) call_args.update(pcall_args) - cmd.extend(prepend.cmd) + # we do not prepend commands used as a 'with' context as they will + # be prepended to any nested commands + if not kwargs.get("_with", False): + cmd.extend(prepend.cmd) cmd.append(self._path) diff --git a/tests/test.py b/tests/test.py index 8ffac583..babaff92 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1101,6 +1101,13 @@ def test_with_context_args(self): out = whoami() self.assertEqual(out.strip(), "") + def test_with_context_nested(self): + echo_path = sh.echo._path + with sh.echo.bake("test1", _with=True): + with sh.echo.bake("test2", _with=True): + out = sh.echo("test3") + self.assertEqual(out.strip(), f"test1 {echo_path} test2 {echo_path} test3") + def test_binary_input(self): py = create_tmp_test( """