zsh-workers
 help / color / mirror / code / Atom feed
* exec -l
@ 2007-04-30  0:52 Dave Yost
  2007-04-30  2:33 ` Geoff Wing
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Dave Yost @ 2007-04-30  0:52 UTC (permalink / raw)
  To: zsh-workers; +Cc: Bob Proulx

Why does zsh not have "exec -l" a la bash?

Thanks

Dave

  - - - - - -

Date: Sun, 29 Apr 2007 18:25:58 -0600
To: Dave Yost <Dave@Yost.com>
Cc: openssh-unix-dev@mindrot.org
Subject: Re: setting current dir of remote shell
From: bob@proulx.com (Bob Proulx)

Dave Yost wrote:
>  Bob Proulx wrote:
>  >For me the following works:
>  >  ssh -t someserver "cd $(pwd) && exec -l \$0"
>  >  $ echo $0
>  >  -bash
>
>  I assume you mean
>
>    ssh -t someserver "cd $(pwd) && exec \$0 -l"

No, I really did mean 'exec -l \$0'.  Your transposition is similar
and will have similar effects but is not identical.  (Very close to
though, and the difference is quite subtle.)

   help exec
   exec: exec [-cl] [-a name] file [redirection ...]
       Exec FILE, replacing this shell with the specified program.
       If FILE is not specified, the redirections take effect in this
       shell.  If the first argument is '-l', then place a dash in the
       zeroth arg passed to FILE, as login does.  If the '-c' option
       is supplied, FILE is executed with a null environment.  The '-a'
       option means to make set argv[0] of the executed process to NAME.
       If the file cannot be executed and the shell is not interactive,
       then the shell exits, unless the shell option 'execfail' is set.

The magic part is:

   "If the first argument is '-l', then place a dash in the zeroth arg
   passed to FILE, as login does."

The classic /bin/login program has always placed a dash as the first
character of the shell when starting up login shells.  Shells inspect
their first argument, their own name, and look for a '-' in the
leading part of the name.  If the shell finds a '-' there then they
assume they were started by login and configure themselves as a login
shell.  Login shells read the defined list of environment files such
as /etc/profile and others.

The transposition of using 'exec \$0 -l' instead of 'exec -l \$0'
won't place a dash in the leading part of the shell's name.  Therefore
the shell won't think it is a login in the same way that it would if
it were really a login shell started by /bin/login.  However the shell
will by the -l option be told to behave as a login shell.  At that
point the shell will behave fully as a login shell and there is no
difference.  However if child programs want to deduce if the parent
shell is a login shell or not they usually look at $0, as I did in my
example, and look to see if the shell's name starts with a dash or
not.  This difference is probably insignificant but child processes
won't be able to tell if the parent is a login shell in that case.

The problem is that POSIX does not specify any options to exec and
therefore use of 'exec -l' is non-standard by definition.  I do not
know which shells support it and which do not.  Bash supports it and
so if it is known to be working with bash then it is fine to use.  But
if it must be completely portable to unspecified shells then of course
it would be a problem.

Meanwhile, the very reason I like it is that 'exec -l $SOMESHELL' can
spawn a random, unknown, unspecified shell as $SOMESHELL and guarantee
that it will be a login shell even if $SOMESHELL does not take the -l
option.  Because of the /bin/login interface of placing a leading dash
all shells will respect that interface.  The classic csh is an example
of a shell that does not accept a -l option to behave as a login
shell.  Therefore 'exec csh -l' would fail while 'exec -l csh' would
succeed in producing csh running as a login shell.  (I think newer csh
variants have added the -l option for compatibility.)

Bob


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

* Re: exec -l
  2007-04-30  0:52 exec -l Dave Yost
@ 2007-04-30  2:33 ` Geoff Wing
  2007-04-30  2:48 ` Phil Pennock
  2007-04-30  4:39 ` Bart Schaefer
  2 siblings, 0 replies; 4+ messages in thread
From: Geoff Wing @ 2007-04-30  2:33 UTC (permalink / raw)
  To: Zsh Hackers

On Monday 2007-04-30 11:15 +1000, Dave Yost output:
:Why does zsh not have "exec -l" a la bash?

"PRECOMMAND MODIFIERS
-      The command is executed with a  `-' prepended to its argv[0] string."

% ssh -t someserver "cd $(pwd) && exec - \$0"

or

% ssh -t someserver "cd $(pwd) && ARGV0=-\$0 exec \$0"


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

* Re: exec -l
  2007-04-30  0:52 exec -l Dave Yost
  2007-04-30  2:33 ` Geoff Wing
@ 2007-04-30  2:48 ` Phil Pennock
  2007-04-30  4:39 ` Bart Schaefer
  2 siblings, 0 replies; 4+ messages in thread
From: Phil Pennock @ 2007-04-30  2:48 UTC (permalink / raw)
  To: zsh-workers

On 2007-04-29 at 17:52 -0700, Dave Yost wrote:
>  Why does zsh not have "exec -l" a la bash?

Because zsh had already introduced its own non-standard way to do the
same thing before bash added its own non-standard extension?  And
nobody's pushed to copy bash's behaviour?  And ksh has yet another way.
So there's no portable method and nobody's yet contributed code to zsh
to copy the interface provided by other shells.

The Single Unix Specification v3 doesn't include any flags for exec.
(Look in the XCU section).  Zsh does not provide any flags.

In zsh, you can put a "-" at the start of any external command by using
the "-" builtin command.  It's the first builtin listed in
zshbuiltins.1.  So, you'd do:

  exec - cmd

so that you exec cmd with argv[0] being "-cmd".  Or just "- cmd" to run
it as a child process.

Alternatively, zsh lets you specify _any_ string as argv[0] for a new
command by ensuring that the variable ARGV0 is set in the environment
when you run it.  So, for instance, you can do:

 ARGV0=-sh exec zsh

to exec zsh with argv[0] set to "-sh", such that zsh will emulate a
POSIX "sh" more closely and do so as a login shell.  This is what in ksh
would be "exec -a -sh zsh".

-Phil


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

* Re: exec -l
  2007-04-30  0:52 exec -l Dave Yost
  2007-04-30  2:33 ` Geoff Wing
  2007-04-30  2:48 ` Phil Pennock
@ 2007-04-30  4:39 ` Bart Schaefer
  2 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2007-04-30  4:39 UTC (permalink / raw)
  To: zsh-workers; +Cc: Bob Proulx

On Apr 29,  5:52pm, Dave Yost wrote:
}
} Why does zsh not have "exec -l" a la bash?

Because it has ARGV0, which is more flexible.

	ARGV0=-csh exec /bin/csh

(to use Bob's example).


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

end of thread, other threads:[~2007-04-30  4:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-04-30  0:52 exec -l Dave Yost
2007-04-30  2:33 ` Geoff Wing
2007-04-30  2:48 ` Phil Pennock
2007-04-30  4:39 ` Bart Schaefer

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