This:
set arrname = ("x" "nx" "y" "ny" "z" "nz")
is an ordinary array assignment (BTW, the quotation marks are not needed in this case).
This:
set arrname = (x,nx,y,ny,z,nz)
also makes $arrname
an array, but it has only one element, with the value x,nx,y,ny,z,nz
(the commas are not special in this context).
This:
set arrname = {x,nx,y,ny,z,nz}
uses a glob pattern; just as {foo,bar}.txt
expands to foo.txt bar.txt
, so {x,nx,y,ny,z,nz}
expands to x nx y ny z nz
.
Apparently tcsh allows you to use {x,nx,y,ny,z,nz}
as an array initialization, though if you expand it yourself to:
set arrname = x nx y ny z nz
it sets $arrname
to just x
and silently ignores the other arguments. This is one of many odd glitches in the way csh and tcsh parse command arguments. I've used csh and tcsh for decades myself (though I've recently switched mostly to bash), and I routinely run into cases like this where I have to experiment to determine how something is going to work.
If you want to set $arrname
to an array value, use parentheses, and don't use commas.
And as barmar mentioned in a comment, you should read this:
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
csh and tcsh do have somewhat more convenient array syntax than bash has. I csh and tcsh, you can refer to the 3rd element of an array as $arrname[3]
; in bash you have to add braces: ${arrname[3]}
. Also, bash arrays are 0-based, while csh/tcsh arrays are 1-based. But the greater consistency of bash's syntax and semantics, IMHO, more than makes up for this.
Some simple array examples, in csh:
% set arr = ( 10 20 30 )
% echo $arr
10 20 30
% echo $arr[3]
30
and in bash:
$ arr=(10 20 30)
$ echo ${arr[@]}
10 20 30
$ echo ${arr[*]}
10 20 30
$ echo ${arr[2]}
30
(The [@]
and [*]
syntax behave subtly differently; see the bash documentation for details.)
$arrname
a single element with the valuex,nx,y,ny,z,nz
. I'm a bit surprised the version with{
braces}
works. – Keith Thompson Jun 27 at 20:40echo ${arrname[2]}
(for example)? Good luck to all – shellter Jun 27 at 20:40