Reputation: 21
I am trying to pass arguments to a bash script from python. The official documentation says that you need to separate with comma but I'm using it and it does not work.
subprocess.run(["/bin/bash test.sh", "hello", "dog", "3" ], shell=True)
In the bash script I have this basic code:
echo "The number of arguments is: $#"
The result is: The number of arguments is: 0
Any advice? I decided it to pass as a string (Not so clean), putting a space " "+"hello" + " dog" + " 3"
Regards,
Upvotes: 2
Views: 1582
Reputation: 820
You have two options.
call it with a list of arguments but without shell=True
:
subprocess.run(["/bin/bash", "test.sh", "hello", "dog", "3" ])
pass a single string with shell=True
:
subprocess.run("/bin/bash test.sh hello dog 3", shell=True)
From docs of subprocess
:
On POSIX with
shell=True
, the shell defaults to/bin/sh
. If args is a string, the string specifies the command to execute through the shell. This means that the string must be formatted exactly as it would be when typed at the shell prompt. This includes, for example, quoting or backslash escaping filenames with spaces in them. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself.
More info about shell
flag: Actual meaning of 'shell=True' in subprocess
Upvotes: 3