0

I would like to copy all the arguments supplied to the Bash script (the special variable $@), in order to export them. I've attempted this code:

### my_script.sh

echo "Case 1:"
for var in "$@"
do
    echo "$var"
done

echo ""
echo "Case 2:"
export a="$@"
for var in $a
do
    echo "$var"
done

Unfortunately, calling it with my_script.sh "abc" "de f" "ghi" "j k" will produce the wrong output: the spaces are breaking the arguments list. How can I copy the arguments of $@?

Case 1:
abc
de f
ghi
j k

Case 2:
abc
de
f
ghi
j
k
Davide_sd
  • 10,578
  • 3
  • 18
  • 30
  • 3
    Possible duplicate of [Copy all script arguments to another variable](https://stackoverflow.com/questions/29536328/copy-all-script-arguments-to-another-variable). Searching doesn't hurt. – Mike Doe Jan 25 '19 at 11:16
  • @emix that partly solved my question. **# 1**: the solution you presented works to copy the scripts arguments, however it is not possible ([or it is complicated](https://stackoverflow.com/questions/5564418/exporting-an-array-in-bash-script)) to export an array. **# 2**: Keep in mind that I have searched for nearly an hour several dozens of threads regarding my question, and your solution didn't come up. I think this is understandable when there are hundreds if not thousands of question regarding bash special variables all slightly different from one another. Thanks anyway. – Davide_sd Jan 25 '19 at 11:59

0 Answers0