0

I have seen similar questions, but in all those answer, you can only get the variable by position. How can one pass a named variable to a bash script such that it is accessible by its name in the bash script?

I have tried to use

os.system('./script.sh --key=val')

But $key didn't have any assigned value in script.sh.

  • See eg https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash/14203146#14203146 – 9769953 Apr 24 '21 at 09:32
  • Note that this has nothing to do with Python. You would have the same issue directly on a command line, in bash itself. – 9769953 Apr 24 '21 at 09:33
  • Also, calling `os.system()` doesn't not guarantee it will use bash functionality: it will run some shell command, and it'll depend on `script.sh` what shell that is. – 9769953 Apr 24 '21 at 09:34
  • @00 Actually, `os.system()` runs `/bin/sh` by definition. This is one of the many, many reasons to prefer `subprocess.run()`, as also suggested in the documentation for `os.system()` – tripleee Apr 24 '21 at 09:36
  • Your question is unclear; perhaps you are actually looking for `subprocess.run(['./script.sh'], env={'key': 'val'}, check=True)` – tripleee Apr 24 '21 at 09:39

0 Answers0