zsh-users
 help / color / mirror / code / Atom feed
From: "Benjamin R. Haskell" <zsh@benizi.com>
To: david sowerby <d_sowerby@yahoo.com>
Cc: "zsh-users@zsh.org" <zsh-users@zsh.org>
Subject: Re: variable/array element question
Date: Mon, 19 Sep 2011 17:15:10 -0400 (EDT)	[thread overview]
Message-ID: <alpine.LNX.2.01.1109191702310.10525@hp.internal> (raw)
In-Reply-To: <1316463747.16563.YahooMailNeo@web36607.mail.mud.yahoo.com>

[-- Attachment #1: Type: TEXT/PLAIN, Size: 1708 bytes --]

On Mon, 19 Sep 2011, david sowerby wrote:

> I did array=( $(ls -l <file>) )
> print $array 
>
> and got an error  Hmmmm......
> did print ${array[2,9]} and got the expected result: elements 2-9
> so zsh didn't like the first element
> Experimenting I did
>
> ls -l <file> | while read one two three four etc ; do print $one $two $three ; done
> same error
>
> So after using zsh for 2 years , I've just discovered that it doesn't 
> like variables to start with '-' .
> Obviously I can use Awk for this example, but I was curious as to why 
> this is?

It's not that it "doesn't like" them... ;-)

Parameter expansion happens before the command is executed.  The 
contents of the array are being interpreted as flags to the 'print' 
builtin.  E.g.

## -l makes `print` print each thing on a separate line
$ array=( -l a b c )
$ print $array
a
b
c
$ print -l a b c
a
b
c
$ array=( -what- this is ridiculous )
$ print $array
zsh: bad option -w

You can get around it with `print` via the '--' (stop processing) 
argument (which you can shorten to a single '-'):

$ array=( -what- this is ridiculous )
$ print - $array
-what- this is ridiculous
$



> I have bash and dash installed and neither have a problem with '-', 
> don't have ksh so don't know about it.

Bash has problems with hyphens in the same places Zsh does (generally). 
The differences are a different set of built-in commands (no 'print', 
AFAICT), and that Zsh treats things as arrays more readily (maybe 
there's an option you can set, but I hate having to type ${array[@]}). 
Example of bash treating an array item as an argument:

$ bash
$ array=( -v What %s earth )
$ printf ${array[@]}
### (no output)
$ echo $What
earth

-- 
Best,
Ben

      reply	other threads:[~2011-09-19 21:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-19 20:22 david sowerby
2011-09-19 21:15 ` Benjamin R. Haskell [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LNX.2.01.1109191702310.10525@hp.internal \
    --to=zsh@benizi.com \
    --cc=d_sowerby@yahoo.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).