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.

2
  • 3
    stackoverflow.com/questions/255898/… Commented Oct 2, 2014 at 20:26
  • Thanks! That was actually really helpful. I can do this whole routine without even using a for loop. Commented Oct 2, 2014 at 20:28

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.