0

I am trying to create a shell script that will take command line arguments and create one or more files based off of that. I know that each command line argument is stored in $0, $1, $2...and so on, so that is what this loop is based on.

for i in $(eval echo {1..$#})
do
    echo "File I'm about to edit/create: "$i""
    touch "$i"
done

However, $i is being taken literally as the number 1, 2..rather than the value in $1.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
user2793442
  • 139
  • 4
  • 13

1 Answers1

2

You would need to use indirect parameter expansion:

echo "File...: ${!i}"
touch "${!i}"

However, it's much simpler to just iterate over the arguments themselves:

for f in "$@"; do
    echo "File...: $f"
    touch "$f"
done
chepner
  • 497,756
  • 71
  • 530
  • 681