How to make a variable containing all the rest parameter from nth particular parameter, for instance the string holds all data from 3rd parameter until 9th parameter is within a variable named $a... Thanks in advance.

  • How should the parameters be separated in the variable? Spaces? Pipe? – Jeff Schaller Feb 15 '18 at 10:56
  • as usual; space that's not surrounded by (double) quotes – abdan Feb 15 '18 at 10:59

In Bash/ksh/zsh, you can use something like ${@:3:7} to get the 3rd to 9th parameters as distinct words, or ${*:3:7} to get them concatenated with the first character of IFS as joiner. (7, since there's seven in total from 3 to 9.)


$ set -- $(seq 11 21)
$ echo "${*:3:7}"
13 14 15 16 17 18 19


$ a="${*:3:7}"

However, if you want to pass those arguments as parameters to another command, use "${@:3:7}" directly on the command line in question, i.e.

somecmd "${@:3:7}"

If you need to do this in standard shell, you can't use ${var:n:m}. Instead you would need to shift the first arguments away, and then use "$@", so:

shift 2
somecmd "$@"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.