2

I have a Bash script to which I am sending 10 arguments.

I'm trying to access the tenth argument, such as echo $10.

Instead I get the first argument with a zero appended.

Any thoughts on how I can access the tenth argument?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Richard
  • 56,349
  • 34
  • 180
  • 251
  • Generally if you want to have more then 2-3 arguments it is good to use flag options instead of using some fixed argument positions to make your code more readable and to remember only flags,not exact positions (like ./script -from xxx -to yyy -num zzz ...). There are some ready open-source libraries for this. Most of them uses bash "shift" command to iterate through them, so they don't usually use more then $3 even if they have dozens of arguments. – XzKto Jul 08 '11 at 08:56

1 Answers1

13

Use ${10} instead of $10. You can use this for a seemingly arbitrary number of arguments. I've tested it up to ${100} successfully.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • 1
    Try this to test: `bash -c "echo \${7854} " $(seq 10 10000)`. – Lynch Jul 07 '11 at 20:31
  • 2
    Don't go looking for the limit: [you'd be busy a long time](http://stackoverflow.com/questions/4185017/maximum-number-of-bash-arguments-max-num-cp-arguments) – sehe Jul 07 '11 at 20:33