zsh-users
 help / color / mirror / code / Atom feed
* Expansion order
@ 2005-01-25 14:41 Mads Martin Joergensen
  2005-01-25 14:53 ` Peter Stephenson
  2005-01-25 15:03 ` Stephane Chazelas
  0 siblings, 2 replies; 4+ messages in thread
From: Mads Martin Joergensen @ 2005-01-25 14:41 UTC (permalink / raw)
  To: zsh-users

Hey together,

$ export LS_OPTIONS="-N --color=tty -T 0"
$ alias ls='ls $LS_OPTIONS'
$ ls
/bin/ls: invalid option --
Try `/bin/ls --help' for more information.

How come the zsh is doing the expansion in this case differently from
csh, tcsh, bash and sh?

If it's a configuration option or such, I would be grateful to know. I
looked through the expansion man-pages, but I might have missed it.

-- 
Mads Martin Joergensen, http://mmj.dk
"Why make things difficult, when it is possible to make them cryptic
 and totally illogical, with just a little bit more effort?"
                                -- A. P. J.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Expansion order
  2005-01-25 14:41 Expansion order Mads Martin Joergensen
@ 2005-01-25 14:53 ` Peter Stephenson
  2005-01-25 16:54   ` Bart Schaefer
  2005-01-25 15:03 ` Stephane Chazelas
  1 sibling, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2005-01-25 14:53 UTC (permalink / raw)
  To: zsh-users

Mads Martin Joergensen wrote:
> Hey together,
> 
> $ export LS_OPTIONS="-N --color=tty -T 0"
> $ alias ls='ls $LS_OPTIONS'
> $ ls
> /bin/ls: invalid option --
> Try `/bin/ls --help' for more information.
> 
> How come the zsh is doing the expansion in this case differently from
> csh, tcsh, bash and sh?

It's sh_word_split again.  You're using $LS_OPTIONS as a single word and
zsh is obliging.  The error message is confusing.

There are various possibilities:

alias ls="ls $LS_OPTIONS"

is the simplest, but if you want changes in LS_OPTIONS to be reflected
later on you need something else.

alias ls='ls $=LS_OPTIONS'

is the obvious.

More sophisticated:

typeset -xT LS_OPTIONS="-N --color=tty -T 0" ls_options
alias ls='ls $ls_options'

This allows for whitespace characters in the options as long as you
manipulate them through the array ls_options.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Expansion order
  2005-01-25 14:41 Expansion order Mads Martin Joergensen
  2005-01-25 14:53 ` Peter Stephenson
@ 2005-01-25 15:03 ` Stephane Chazelas
  1 sibling, 0 replies; 4+ messages in thread
From: Stephane Chazelas @ 2005-01-25 15:03 UTC (permalink / raw)
  To: zsh-users

On Tue, Jan 25, 2005 at 03:41:50PM +0100, Mads Martin Joergensen wrote:
> Hey together,
> 
> $ export LS_OPTIONS="-N --color=tty -T 0"
> $ alias ls='ls $LS_OPTIONS'
> $ ls
> /bin/ls: invalid option --
> Try `/bin/ls --help' for more information.
> 
> How come the zsh is doing the expansion in this case differently from
> csh, tcsh, bash and sh?
> 
> If it's a configuration option or such, I would be grateful to know. I
> looked through the expansion man-pages, but I might have missed it.
[...]

See question 3.1 in the FAQ
http://zsh.sunsite.dk/FAQ/zshfaq03.html#l17

Even with other Bourne like shells, it's not a good idea to
write it this way as it relies on the current value of IFS.
(if you change IFS, your ls will stop working).

Here, you want LS_OPTIONS to be an array, not a string.

LS_OPTIONS=(-N --color=tty -T 0)
alias ls='ls "${LS_OPTIONS[@]"'

Or if LS_OPTIONS has to be an environment variable, then you'd
better ensure the the right value of IFS is used:

export LS_OPTIONS='-N,--color=tty,-T,0'
ls() {
  (
    IFS=,
    set -f
    exec command ls $LS_OPTIONS "$@"
  )
}

or for zsh, where word splitting and filename generation are
thanksfully not implicit:

export LS_OPTIONS='-N,--color=tty,-T,0'
ls() {
  local IFS=,
  command ls $=LS_OPTIONS "$@"
}

" " (space) is not a good choice of <character> for a
<character> separated list, as it doesn't allows empty elements,
that's why I used "," instead above.

Alternatively, you may consider "LS_OPTIONS" as a part of a
shell command line, so that can have:

LS_OPTIONS='-foo "arg with spaces" -empty ""'
or
LS_OPTIONS='--color="$([ -t 1 ] && echo yes || echo no)"'

Then, you can use it as:

ls() {
  eval "command ls $LS_OPTIONS \"\$@\""
}

Which should work in zsh as well as in POSIX shells.

-- 
Stéphane


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Expansion order
  2005-01-25 14:53 ` Peter Stephenson
@ 2005-01-25 16:54   ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2005-01-25 16:54 UTC (permalink / raw)
  To: zsh-users

On Jan 25,  2:53pm, Peter Stephenson wrote:
} Subject: Re: Expansion order
}
} typeset -xT LS_OPTIONS="-N --color=tty -T 0" ls_options
} alias ls='ls $ls_options'
} 
} This allows for whitespace characters in the options as long as you
} manipulate them through the array ls_options.

Doesn't that need to be

  typeset -xT LS_OPTIONS="-N --color=tty -T 0" ls_options ' '

??  Otherwise it's splitting on colons, not spaces.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2005-01-26 17:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-25 14:41 Expansion order Mads Martin Joergensen
2005-01-25 14:53 ` Peter Stephenson
2005-01-25 16:54   ` Bart Schaefer
2005-01-25 15:03 ` Stephane Chazelas

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).