[comp.unix.programmer] Problem: aliases and exec in shells

evans@decvax.dec.com (Marc Evans) (10/25/90)

Hi -

I made an interresting discovery concerning the use of exec in shells. 
Specifically, I expected that something of the form 'exec foo' would look
through whatever search rules that would normally be used as though I had
used 'foo', and then replace my current process with what it found. This
in fact is not always the case.

In both ksh and csh, on many different vendors OS's that I have tried, if
foo is an alias for something, the alias is not used. For example, consider
the following:

	% csh
	% alias ls 'ls -F'
	% exec ls

The result would be simply the results of running the ls command without
any arguments, rather than running the ls command with the argument -F.

The manuals and books that I have looked through don't define what the
behavior is suppose to be in this case, and looking through the sources
shows that aliases are in fact ignored. However, ksh functions could be
used, which provides for a reasonable hack-around.

Does the current behavior seem intuitive to people? Am I just expecting
something which I really shouldn't be? Your opinions are desired as I am
debating if I will ask certain vendors to change the behavior to pay
attention to the alias if it exists.

- Marc

-- 
===========================================================================
Marc Evans - WB1GRH - evans@decvax.DEC.COM  | Synergytics     (603)635-8876
      Unix and X Software Contractor        | 21 Hinds Ln, Pelham, NH 03076
===========================================================================

chet@cwns1.INS.CWRU.Edu (Chet Ramey) (10/26/90)

In article <427@decvax.decvax.dec.com.UUCP> evans@decvax.DEC.COM writes:

[when the word to be aliased is not in a `command position']

>In both ksh and csh, on many different vendors OS's that I have tried, if
>foo is an alias for something, the alias is not used. For example, consider
>the following:
>
>	% csh
>	% alias ls 'ls -F'
>	% exec ls

[the `ls' is not expanded]

A simple work around for both ksh and bash is the following:

alias exec="exec "

Then the word following the `exec' will be checked for aliases and
expanded if necessary.  `nohup' is handled the same way.

I don't think that all words of a command should be checked for aliases
and expanded.

Chet
-- 
Chet Ramey			``As I recall, Doug was keen on boxing.  But
Network Services Group		  when he learned to walk, he took up puttin'
Case Western Reserve University	  the boot in the groin.''
chet@ins.CWRU.Edu

subbarao@phoenix.Princeton.EDU (Kartik Subbarao) (10/26/90)

In article <427@decvax.decvax.dec.com.UUCP> evans@decvax.DEC.COM writes:
>	% csh
>	% alias ls 'ls -F'
>	% exec ls
>
>The result would be simply the results of running the ls command without
>any arguments, rather than running the ls command with the argument -F.
>
>The manuals and books that I have looked through don't define what the
>behavior is suppose to be in this case, and looking through the sources
>shows that aliases are in fact ignored. 

>Does the current behavior seem intuitive to people? Am I just expecting
>something which I really shouldn't be? Your opinions are desired as I am
>debating if I will ask certain vendors to change the behavior to pay
>attention to the alias if it exists.

Well..... the problem might not be as bad as the shell's handling of 'exec'.
I think it just has to do with alias interpretation.

The thing with aliases is that the shell will only interpret an alias like
alias ls 'ls -F' if it's the first thing in the command line. (or the first
thing after a "|" , or enclosed in a ``, or the like something.) So even after 
alias foo '| more', and typed w foo, it would certainly not do w | more.

It WOULD be nice if csh had a "global" alias option, but then there's the 
chicken and egg problem of resourcing your global alias file after you've
already done so. 

i.e 

alias foo '| more' 

is a global alias. The next time you sourced this file it might be interpreted
as 

alias '| more' '|more' which would make no sense.

I guess this could be turned off during alias addition. Any other ideas?


				-Kartik









>
>- Marc
>
>-- 
>===========================================================================
>Marc Evans - WB1GRH - evans@decvax.DEC.COM  | Synergytics     (603)635-8876
>      Unix and X Software Contractor        | 21 Hinds Ln, Pelham, NH 03076
>===========================================================================



(I need a new .signature -- any suggestions?)
subbarao@{phoenix or gauguin}.Princeton.EDU -|Internet
kartik@silvertone.Princeton.EDU (NeXT mail)       -|	
SUBBARAO@PUCC.BITNET			          - Bitnet

john@sco.COM (John R. MacMillan) (11/02/90)

evans@decvax.dec.com (Marc Evans) writes:

|In both ksh and csh, on many different vendors OS's that I have tried, if
|foo is an alias for something, the alias is not used. For example, consider
|the following:
|
|	% csh
|	% alias ls 'ls -F'
|	% exec ls
|
|The result would be simply the results of running the ls command without
|any arguments, rather than running the ls command with the argument -F.

In the ksh manual, you'll probably find this behaviour documented in
the section on alias expansion.  Alias expansion is performed on the
first word, but not its arguments, barring some exceptions.  One
exception can get you the behaviour you want:  if the first word is an
alias whose expanded value ends in a space, alias expansion is
performed on the next word.  So you can get the behaviour you want in
ksh with:

$ ksh
$ alias ls='ls -F'
$ alias exec='exec '
$ exec ls

Also handy for nohup.
-- 
John R. MacMillan       | I'm in love with a McDonald's girl
SCO Canada, Inc.        | She's an angel in a polyester uniform.
john@sco.COM            |      -- barenaked ladies