Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.
2 added 1026 characters in body
source | link

However, I don't know what data structure $@ is.

It's a special parameter that expands to the values of the positional parameters... But that's nitpicking about the terminology.

TakingWe can view the positional parameters as parts of $@, so it has a number of distinct elements ($1, $2...), that can be accessed independently and are named by consecutive natural numbers. That would makemakes it seem a lot like something that is usually called an arrayarray.

The syntax is a bit weird, though, and even limited. There's no way to modify a single element of the array at a timeindividually. Instead, the whole thing has to be set at once. (You can use set -- "$@" foo to append a value, or set -- "${@:1:2}" foo "${@:3}" in some shells, to add a value in the middle, but. But you can't setin both cases you have to write out the elements individuallywhole resulting list.)

Why it behave differently with $* when including in double quote,

Because they're defined to behave differently.

However, it can also echoed entirely with simple echo $@, if it is an array, only first element will be shown.

If you mean the fact that a=(foo bar asdf); echo $a will output just foo, then this is mostly a quirk of the shell syntax, and the fact that ksh-style named arrays were created later than the positional parameters and $@. Plain $a is the same as ${a[0]} so it has the backward-compatible meaning of a single scalar value, regardless of if a is an array or a simple scalar variable.

The @ sign referring to the whole list was reused with named arrays in that "${a[@]}" is the way to get the whole list. Compared to named arrays, with $@, the unnecessary braces and brackets and the name are just skipped.

Or in other words, I want to know how $@ stored in computer memory.

That depends on the implementation, you'll have to look the source code of any particular shell you care about.

Is it a string, a multi-line string or a array?

An array, mostly. Though different from the ksh-style named arrays, since they can have arbitrary nonnegative integers as indexes, not just consecutive ones as with $@. (That is, a named array can be sparse, and have e.g. the indexes 1, 3 and 4, with 0 and 2 missing. That's not possible with the positional parameters.)

It's not a single string, since it can be expanded to distinct elements, and calling the elements lines is also not right, since any regular variable, or one of the positional parameters (elements of $@) can also contain newlines.

If it is a unique data type, is it possible to define a custom variable as an instance of this type?

No. But named arrays are probably more useful anyway.

However, I don't know what data structure $@ is.

It's a special parameter that expands to the values of the positional parameters...

Taking the positional parameters as parts of $@, it has a number of distinct elements ($1, $2...), that can be accessed independently and are named by consecutive natural numbers. That would make it seem a lot like something called an array.

The syntax is a bit weird, though, and even limited. There's no way to modify a single element of the array at a time, the whole thing has to be set at once. (You can use set -- "$@" foo to append a value, or set -- "${@:1:2}" foo "${@:3}" in some shells, to add a value in the middle, but you can't set the elements individually.)

Why it behave differently with $* when including in double quote,

Because they're defined to behave differently.

Or in other words, I want to know how $@ stored in computer memory.

That depends on the implementation, you'll have to look the source code of any particular shell you care about.

Is it a string, a multi-line string or a array?

An array, mostly. Though different from the ksh-style named arrays, since they can have arbitrary nonnegative integers as indexes, not just consecutive ones as with $@.

It's not a single string, since it can be expanded to distinct elements, and calling the elements lines is also not right, since any regular variable, or one of the positional parameters (elements of $@) can also contain newlines.

If it is a unique data type, is it possible to define a custom variable as an instance of this type?

No.

However, I don't know what data structure $@ is.

It's a special parameter that expands to the values of the positional parameters... But that's nitpicking about the terminology.

We can view the positional parameters as parts of $@, so it has a number of distinct elements ($1, $2...), that can be accessed independently and are named by consecutive natural numbers. That makes it something that is usually called an array.

The syntax is a bit weird, though, and even limited. There's no way to modify a single element of the array individually. Instead, the whole thing has to be set at once. (You can use set -- "$@" foo to append a value, or set -- "${@:1:2}" foo "${@:3}" to add a value in the middle. But you in both cases you have to write out the whole resulting list.)

Why it behave differently with $* when including in double quote,

Because they're defined to behave differently.

However, it can also echoed entirely with simple echo $@, if it is an array, only first element will be shown.

If you mean the fact that a=(foo bar asdf); echo $a will output just foo, then this is mostly a quirk of the shell syntax, and the fact that ksh-style named arrays were created later than the positional parameters and $@. Plain $a is the same as ${a[0]} so it has the backward-compatible meaning of a single scalar value, regardless of if a is an array or a simple scalar variable.

The @ sign referring to the whole list was reused with named arrays in that "${a[@]}" is the way to get the whole list. Compared to named arrays, with $@, the unnecessary braces and brackets and the name are just skipped.

Or in other words, I want to know how $@ stored in computer memory.

That depends on the implementation, you'll have to look the source code of any particular shell you care about.

Is it a string, a multi-line string or a array?

An array, mostly. Though different from the ksh-style named arrays, since they can have arbitrary nonnegative integers as indexes, not just consecutive ones as with $@. (That is, a named array can be sparse, and have e.g. the indexes 1, 3 and 4, with 0 and 2 missing. That's not possible with the positional parameters.)

It's not a single string, since it can be expanded to distinct elements, and calling the elements lines is also not right, since any regular variable, or one of the positional parameters (elements of $@) can also contain newlines.

If it is a unique data type, is it possible to define a custom variable as an instance of this type?

No. But named arrays are probably more useful anyway.

1
source | link

However, I don't know what data structure $@ is.

It's a special parameter that expands to the values of the positional parameters...

Taking the positional parameters as parts of $@, it has a number of distinct elements ($1, $2...), that can be accessed independently and are named by consecutive natural numbers. That would make it seem a lot like something called an array.

The syntax is a bit weird, though, and even limited. There's no way to modify a single element of the array at a time, the whole thing has to be set at once. (You can use set -- "$@" foo to append a value, or set -- "${@:1:2}" foo "${@:3}" in some shells, to add a value in the middle, but you can't set the elements individually.)

Why it behave differently with $* when including in double quote,

Because they're defined to behave differently.

Or in other words, I want to know how $@ stored in computer memory.

That depends on the implementation, you'll have to look the source code of any particular shell you care about.

Is it a string, a multi-line string or a array?

An array, mostly. Though different from the ksh-style named arrays, since they can have arbitrary nonnegative integers as indexes, not just consecutive ones as with $@.

It's not a single string, since it can be expanded to distinct elements, and calling the elements lines is also not right, since any regular variable, or one of the positional parameters (elements of $@) can also contain newlines.

If it is a unique data type, is it possible to define a custom variable as an instance of this type?

No.