简体   繁体   中英

Iterate the arguments in shell script ( arguments sent as a list from python file )

I have tried below:

param_list = ("hi ", "life", "jam", "hello")

print param_list

subprocess.call(shlex.split('./test.sh *param_list'))

In my shell script, I want to loop through the list that I passed:

#!/bin/sh


for i in $*; do
  echo $i
done

But the above code prints *param_list not the ("hi ", "life", "jam", "hello"). Can someone help me with this requirement !!

Others have discussed the python side. For the shell, you need to use

for arg in "$@"; do

which will iterate over the args precisely as given, without being affected by word splitting or filename expansion.

To see the difference, try this:

# set positional parameters
set -- "first arg" "second arg" "*"

# Now, echo the args
for arg in "$@"; do echo "$arg"; done

# versus
for word in $*; do echo "$word"; done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM