0

This question already has an answer here:

setup:

$ echo $0
-bash

$ ls
pippo1  pippo2  pippo3  test.sh

script code:

$ cat test.sh
#!/bin/ksh

PAYLOAD=( $1 )
echo "============================"
echo -e "Payload:\t${PAYLOAD[@]}"
echo "============================"

for payload in "${PAYLOAD[@]}"
do
    echo "$payload"
done

output:

$ ./test.sh pippo*
============================
Payload:    pippo1
============================
pippo1


$ ./test.sh "pippo*"
============================
Payload:    pippo1 pippo2 pippo3
============================
pippo1
pippo2
pippo3

Desired output is #2, launching the script with #1 string (without double quotes).

How can I modify test.sh to achieve it?

EDIT Working code:

#!/bin/ksh

PAYLOAD=( "$@" )
echo "============================"
echo -e "Payload:\t${PAYLOAD[@]}"
echo "============================"

for payload in "${PAYLOAD[@]}"
do
    echo "$payload"
done

marked as duplicate by Stephen Harris, Eric Renouf, sam, Julie Pelletier, Shadur Aug 26 '16 at 18:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    huh...why not PAYLOAD=("${@}") and be done with it – iruvar Aug 26 '16 at 15:26
  • 1
    double: unix.stackexchange.com/questions/305927/… (and a little warning: you are using ksh, not bash!) – Ipor Sircer Aug 26 '16 at 15:27
  • @iruvar because this is a snippet, the whole code contains other arguments ;) – Dariopnc Aug 26 '16 at 15:48
  • @Dariopnc, you need to extract a subset of arguments to put into PAYLOAD, you could use "${@:start:length}" – iruvar Aug 26 '16 at 15:52
  • probably double indeed, @IporSircer I thought the parameter expansion was happening outside the script, in the echo $0 shell – Dariopnc Aug 26 '16 at 16:01