1

My goal is to run a shell script on my Mac Terminal that passes command line arguments to a python script. I know how to do it on Windows:

Batch file (pythonScript.bat):

@py.exe C:\path\to\my\pythonScript.py %*
@pause

The %* forwards any command line arguments entered after the batch filename to the Python script. So when I run pythonScript.bat from the command line...

C:\> pythonScript

...the arguments I enter after the batch filename (for example C:> pythonScript some arguments) are passed to the python script.

How can I do that on my Mac?

How can I complement my code so that any command line arguments are forwarded to the python script?

Shell script (pythonScript.command):

#!/usr/bin/env bash
python3 /path/to/my/pythonScript.py
tripleee
  • 175,061
  • 34
  • 275
  • 318
aurumpurum
  • 932
  • 3
  • 11
  • 27

1 Answers1

1

in Linux bash I would do:

#!/usr/bin/env bash
python3 /path/to/my/pythonScript.py "$@"

$@ is interpreted as the list of arguments passed to the bash script.

I haven't got a mac to test it, but worth trying it

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Jayvee
  • 10,670
  • 3
  • 29
  • 40
  • That works! The echo command is not needed in my opinion. So just python3 /path/to/my/pythonScript.py $@ works (without quotes). Thanks a lot! I was searching the web for quite a while and couldn't find good info! – aurumpurum May 15 '21 at 21:09
  • 3
    `"$@"` needs quoting, otherwise it will break if the arguments contain whitespace or shell metacharacters. – tripleee May 17 '21 at 14:10
  • As already suggested, probably get rid of the [useless `echo`](http://www.iki.fi/era/unix/award.html#echo) too. – tripleee May 17 '21 at 15:01
  • `bash` is `bash`; this isn't specific to Linux. You don't need the backquotes, either. – chepner May 17 '21 at 15:07
  • And get rid of the pointless backticks! – William Pursell May 17 '21 at 15:12
  • The backticks aren't just pointless, they introduce bugs you wouldn't otherwise have. Putting the backticks there means you're capturing the Python script's output, splitting it into words, expanding any word that looks like a glob, and then running the resulting list of items as a second command after the Python interpreter exits. They are abjectly, completely, unambiguously wrong. – Charles Duffy Mar 19 '22 at 12:26