I have a very simple shell script which accepts 2 arguments after some processing pass these 2 arguments to python code. like below
arg1=$1
arg2=$2
#some processing...
python3.6 abc.py $arg1 $arg2
Now the requirement is my shell script will accept variable number of arguments and it should pass same to python program. For accepting I can use
for arg in "$@" ; do
#use array for assignments
done
but issue is how to pass it to python program.
You don't iterate over $@
at all; you simply pass all contained arguments directly, with
python3.6 abc.py "$@"
If the arguments are, for example, 1 and 2, this results in
python3.6 abc.py "1" "2"
not
python3.6 abc.py "1 2".
This is intentionally special behavior of quoted $@
, designed specifically for this purpose. If you really wanted the latter, you'd use "$*"
instead.
If you need an arbitrary number of values created from the values in $@
, you'll need an explicit array.
for x in "$@"; do
# do some work on x, add the result y to the array
arr+=("$y")
done
python3.6 abc.py "${arr[@]}"