0

The code block in particular is:

for n in {1..$PASSEDARGS}; do
      printf "%s\n" "$n"
done

The following code block doesn't work, instead it prints {1..3} where 3 is the value of the number of arguments passed to the script.

How do I make the program print the argument at $n?

2 Answers 2

0

Using the C-like for-loop syntax of Bash:

for (( i = 1; i <= PASSEDARGS; ++i )); do
  printf "%s\n" "$i"
done
2
  • I was thinking this approach, but I wanted to keep with the bracket expansion method. This one should suffice. Commented Jan 4, 2017 at 10:48
  • @guest_2019238121 I was uncertain whether you wanted the numbers or the values of the positional parameters. If you want the values of $1, $2, etc., then just iterate over "$@". Commented Jan 4, 2017 at 10:51
-1
for n in $(seq $#); do
      printf "%s\n" "$n"
      eval echo argument at $n: \$$n
done
1
  • 1
    Wow, eval of an echo of an unquoted, unsanitized, variable! Try that with set - "hello;ls". Commented Jan 4, 2017 at 10:38

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.