Programming
python bash shell argparse
Updated Tue, 16 Aug 2022 03:13:03 GMT

How to pass an argument to a shell script?


I have a shell script that points to a Python script.

script.sh

#!/bin/bash
python /folder/script.py

The Python script takes an argument, for example script.py -d 20210107

I tried running the shell script with the date parameter like script.sh -d 20210107 but this is not working. Do I need to add something to my shell script to fix this?




Solution

#!/bin/bash
python /folder/script.py "$@"

Pass arguments from upper script to lower script with $@.





Comments (3)

  • +0 – @CharlesDuffy, ok added quotes. — Jan 11, 2021 at 21:53  
  • +0 – what is the difference between $* and $@ ? — Jan 11, 2021 at 22:13  
  • +1 – @AviG The latter correctly preserves inputs that were quoated and have spaces in them. The former basically gives you what you would have had if there the arguments were written with no quoting - so one arument with a space in it could be treated as two arguments. — Jan 11, 2021 at 22:54