1

I have just started reading about "$@" and "$*", I wanted to know if I can specifically point to an element in the "$@" array. Like without using any loop, I want to be able to pick element number 3 from "$@". Is there a way of doing this like "$1+@" or something like this? I already know about "${1}" but want to know specifically about "$@" and "$*". I tried searching for it but did not find anything related to this.

  • 1
    array=(apple banana orange); printf "%s\n" "${array[1]}" – jasonwryan Jul 12 '17 at 0:40
  • @jasonwryan Thanks, it worked, doesn't any syntax something specifically like "$1+@" or "$@+1" exist? I think I saw it somewhere, but am not sure because it has been a lot of time. – GypsyCosmonaut Jul 12 '17 at 1:02
1

It is said that the positional parameters are not an array.
And the way that exists to set them is via set. No other array needs that.

$ set -- one two t33 f44
$ printf '%s\n' "$@"
one
two
t33
f44

But at least in bash (and ksh and zsh), they could be selected just as easy:

$ set -- one two t33 f44
$ echo "${@:2:1}"
two
$ echo "${@:2:2}"
two t33
  • If echo echo ${@:2:1} gives the output two. So does this mean that one two t33 f44 are set at the indexes 1 2 3 4 respectively instead of usual format 0 1 2 3 considered in arrays? – GypsyCosmonaut Jul 12 '17 at 1:24
  • And also, why does echo ${@:0:1} gives the output similar to echo $0 i.e the current shell name? – GypsyCosmonaut Jul 12 '17 at 1:30
  • 1
    Yes!. That's the usual numbering of positional parameters as $0 is (most of the time) the name of the running script/program. Just do an echo $0 to see it. @GypsyCosmonaut – Arrow Jul 12 '17 at 1:31
  • 1
    Well, as the zero index is the name of the executing script/shell, it must be what should be printed by asking for it with echo ${@:0:1}. Doesn't it? @GypsyCosmonaut – Arrow Jul 12 '17 at 1:33
  • 1
    ${parameter:offset:length} is substring expansion in general, but for @ it is treated differently: ${@:offset:length}you get length positional parameters beginning at offset. – NickD Jul 12 '17 at 1:34
1

$@ is not an array: it's just a list of the arguments. In bash, you can create an array, initialize it with the values from $@ and then use indexing:

declare -a foo=($@)

echo ${foo[2]}

The array indices start from 0, so the above prints the third argument to the script.

0

Essentially, you can't. The thing is that $* and $@ are not arrays; they are simple variables. Thus, it isn't possible to index them.

Their values are just strings, defined in slightly different ways. $1, $2, etc. give you access to the individual components.

  • 1
    I believe that you should take a look to my answer. There is an easy way of using indexes to read elements of the positional parameters (at least on some shells) – Arrow Jul 12 '17 at 1:17
  • My answer does not conflict with that. I was correcting the assumption in the question that the parameters are arrays, that's all. – Bob Eager Jul 12 '17 at 6:38

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.