zsh-users
 help / color / mirror / code / Atom feed
* allow zsh to source a script and then become interactive
@ 2017-04-14 16:55 Anthony Fletcher
  2017-04-14 20:28 ` Bart Schaefer
  2017-04-14 22:41 ` Nikolay Aleksandrovich Pavlov (ZyX)
  0 siblings, 2 replies; 10+ messages in thread
From: Anthony Fletcher @ 2017-04-14 16:55 UTC (permalink / raw)
  To: zsh-users

This is an oldie but goodie that I don't have a solution to. How can I
invoke a zsh that sources a particular file (not the standard start up
files) and then become interactive, preserving functions, etc?

This is akin to the ksh invocation:

 ENV=startup ksh

or bash's

 bash --rcfile startup

One solution was covered in 2005 by Bart: see
http://www.zsh.org/mla/users/2005/msg00600.html However this only works
if you can change the .zshrc file.

Another alternative is to create a custom .zshrc in a temporary
directory and set the environment variable ZDOTDIR. But this is a little
clunky.

I look after an increasing disparate collection of systems and I'd
like to launch a customised zsh on a remote system without changing
anything ahead of time.

Any ideas or should we propose a --rcfile option to zsh?

		Anthony.




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

* Re: allow zsh to source a script and then become interactive
  2017-04-14 16:55 allow zsh to source a script and then become interactive Anthony Fletcher
@ 2017-04-14 20:28 ` Bart Schaefer
  2017-04-14 22:03   ` Anthony Fletcher
  2017-04-14 22:41 ` Nikolay Aleksandrovich Pavlov (ZyX)
  1 sibling, 1 reply; 10+ messages in thread
From: Bart Schaefer @ 2017-04-14 20:28 UTC (permalink / raw)
  To: zsh-users

On Apr 14, 12:55pm, Anthony Fletcher wrote:
}
} This is an oldie but goodie that I don't have a solution to. How can I
} invoke a zsh that sources a particular file (not the standard start up
} files) and then become interactive, preserving functions, etc?

This should do it:

    zsh -is <<<"source $aparticularfile </dev/tty && exec </dev/tty"


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

* Re: allow zsh to source a script and then become interactive
  2017-04-14 20:28 ` Bart Schaefer
@ 2017-04-14 22:03   ` Anthony Fletcher
  2017-04-15  1:00     ` Daniel Shahaf
  0 siblings, 1 reply; 10+ messages in thread
From: Anthony Fletcher @ 2017-04-14 22:03 UTC (permalink / raw)
  To: zsh-users

Thanks Bart. 

That's cool. However this gives one PS1 prompt as extra output. So I
have to set PS1='' before I call zsh and then set PS1 inside the
payload script.

Demo: Create the setup file

% cat >/tmp/setup <<END
PS1='>>> '
date
date
xxxx () { echo "hello"; }
END

Run zsh - added -f to ignore my start up files

% zsh -fis <<<"source /tmp/setup </dev/tty && exec </dev/tty"
teapot% Fri Apr 14 17:53:13 EDT 2017                                            
Fri Apr 14 17:53:13 EDT 2017
>>> xxxx                                                                        
hello
>>>                                                                             

See the extra "teapot% " prompt that sneaks in. Instead run

PS1='' zsh -fis <<<"source /tmp/setup </dev/tty && exec </dev/tty"

and it does the desired thing. Brilliant.

		Anthony. :-)


On 14 Apr 2017 at 13:28:58, Bart Schaefer wrote:
> On Apr 14, 12:55pm, Anthony Fletcher wrote:
> }
> } This is an oldie but goodie that I don't have a solution to. How can I
> } invoke a zsh that sources a particular file (not the standard start up
> } files) and then become interactive, preserving functions, etc?
> 
> This should do it:
> 
>     zsh -is <<<"source $aparticularfile </dev/tty && exec </dev/tty"
> 


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

* Re: allow zsh to source a script and then become interactive
  2017-04-14 16:55 allow zsh to source a script and then become interactive Anthony Fletcher
  2017-04-14 20:28 ` Bart Schaefer
@ 2017-04-14 22:41 ` Nikolay Aleksandrovich Pavlov (ZyX)
  2017-04-14 23:14   ` Bart Schaefer
  1 sibling, 1 reply; 10+ messages in thread
From: Nikolay Aleksandrovich Pavlov (ZyX) @ 2017-04-14 22:41 UTC (permalink / raw)
  To: Anthony Fletcher, zsh-users

14.04.2017, 19:57, "Anthony Fletcher" <anthony@bifb.org>:
> This is an oldie but goodie that I don't have a solution to. How can I
> invoke a zsh that sources a particular file (not the standard start up
> files) and then become interactive, preserving functions, etc?
>
> This is akin to the ksh invocation:
>
>  ENV=startup ksh
>
> or bash's
>
>  bash --rcfile startup
>
> One solution was covered in 2005 by Bart: see
> http://www.zsh.org/mla/users/2005/msg00600.html However this only works
> if you can change the .zshrc file.
>
> Another alternative is to create a custom .zshrc in a temporary
> directory and set the environment variable ZDOTDIR. But this is a little
> clunky.
>
> I look after an increasing disparate collection of systems and I'd
> like to launch a customised zsh on a remote system without changing
> anything ahead of time.
>
> Any ideas or should we propose a --rcfile option to zsh?
>
>                 Anthony.

I tried to play with `ENV==(<<< $'emulate zsh\necho abc') ARGV0=sh zsh`, but apparently this does not work: e.g. I do not see `$path` variable in this case, though I do see `abc`. Guess $ENV is too late to switch emulation mode and have all necessary initialization steps be performed.


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

* Re: allow zsh to source a script and then become interactive
  2017-04-14 22:41 ` Nikolay Aleksandrovich Pavlov (ZyX)
@ 2017-04-14 23:14   ` Bart Schaefer
  2017-04-14 23:39     ` Anthony Fletcher
  0 siblings, 1 reply; 10+ messages in thread
From: Bart Schaefer @ 2017-04-14 23:14 UTC (permalink / raw)
  To: zsh-users

On Apr 15,  1:41am, Nikolay Aleksandrovich Pavlov (ZyX) wrote:
}
} I tried to play with
} `ENV==(<<< $'emulate zsh\necho abc') ARGV0=sh zsh`,
} but apparently this does not work: e.g. I do not see `$path` variable
} in this case, though I do see `abc`. Guess $ENV is too late to
} switch emulation mode and have all necessary initialization steps be
} performed.

Using "emulate" only changes setopts, it doesn't re-initialize other
state that would be in place before the startup files are sourced.

Good idea, though -- it might work for Anthony's case.  I'd recommend
"emulate -R zsh" for more thorough option reset.


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

* Re: allow zsh to source a script and then become interactive
  2017-04-14 23:14   ` Bart Schaefer
@ 2017-04-14 23:39     ` Anthony Fletcher
  0 siblings, 0 replies; 10+ messages in thread
From: Anthony Fletcher @ 2017-04-14 23:39 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users


On 14 Apr 2017 at 16:14:45, Bart Schaefer wrote:
> On Apr 15,  1:41am, Nikolay Aleksandrovich Pavlov (ZyX) wrote:
> }
> } I tried to play with
> } `ENV==(<<< $'emulate zsh\necho abc') ARGV0=sh zsh`,
> } but apparently this does not work: e.g. I do not see `$path` variable
> } in this case, though I do see `abc`. Guess $ENV is too late to
> } switch emulation mode and have all necessary initialization steps be
> } performed.
> 
> Using "emulate" only changes setopts, it doesn't re-initialize other
> state that would be in place before the startup files are sourced.
> 
> Good idea, though -- it might work for Anthony's case.  I'd recommend
> "emulate -R zsh" for more thorough option reset.

Sadly not. One of my start-up files contains

  typeset -U path PATH
  path[(r).]=() 

and this gives the error "path: attempt to assign array value to
non-array". So it isn't completely in zsh mode. Playing further I
provoked a segmentation fault! This dies:

   ARGV0=sh zsh -c 'typeset -U path PATH'

(zsh version 5.2.6 on Fedora).

			Anthony.


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

* Re: allow zsh to source a script and then become interactive
  2017-04-14 22:03   ` Anthony Fletcher
@ 2017-04-15  1:00     ` Daniel Shahaf
  2017-04-15  2:45       ` Bart Schaefer
  0 siblings, 1 reply; 10+ messages in thread
From: Daniel Shahaf @ 2017-04-15  1:00 UTC (permalink / raw)
  To: zsh-users

Anthony Fletcher wrote on Fri, Apr 14, 2017 at 18:03:42 -0400:
> See the extra "teapot% " prompt that sneaks in. Instead run
> 
> PS1='' zsh -fis <<<"source /tmp/setup </dev/tty && exec </dev/tty"
> 
> and it does the desired thing. Brilliant.

You must be using an older version.  In current versions, PS1 is no
longer read from the environment, so you should set it to empty inside
the here string.


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

* Re: allow zsh to source a script and then become interactive
  2017-04-15  1:00     ` Daniel Shahaf
@ 2017-04-15  2:45       ` Bart Schaefer
  2017-04-15 15:40         ` Daniel Shahaf
  0 siblings, 1 reply; 10+ messages in thread
From: Bart Schaefer @ 2017-04-15  2:45 UTC (permalink / raw)
  To: zsh-users

On Apr 15,  1:00am, Daniel Shahaf wrote:
} Subject: Re: allow zsh to source a script and then become interactive
}
} Anthony Fletcher wrote on Fri, Apr 14, 2017 at 18:03:42 -0400:
} > See the extra "teapot% " prompt that sneaks in. Instead run
} > 
} > PS1='' zsh -fis <<<"source /tmp/setup </dev/tty && exec </dev/tty"
} > 
} > and it does the desired thing. Brilliant.
} 
} You must be using an older version.  In current versions, PS1 is no
} longer read from the environment, so you should set it to empty inside
} the here string.

Setting it in the here-string is too late.  The prompt is printed even
before the here-string is read.

However, I tried it, and PS1 is imported by zsh built from current git.

The parameters not imported are:
    _
    *
    #
    EGID
    EUID
    GID
    histchars
    HISTCHARS
    IFS
    KEYBOARD_HACK
    module_path
    OPTIND
    path
    PS4
    TRY_BLOCK_ERROR
    TRY_BLOCK_INTERRUPT
    UID
    USERNAME
    zsh_eval_context


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

* Re: allow zsh to source a script and then become interactive
  2017-04-15  2:45       ` Bart Schaefer
@ 2017-04-15 15:40         ` Daniel Shahaf
  2017-04-15 19:06           ` Anthony Fletcher
  0 siblings, 1 reply; 10+ messages in thread
From: Daniel Shahaf @ 2017-04-15 15:40 UTC (permalink / raw)
  To: zsh-users

Bart Schaefer wrote on Fri, Apr 14, 2017 at 19:45:30 -0700:
> However, I tried it, and PS1 is imported by zsh built from current git.

Ah, my bad.  (I normally run Src/zsh with a custom zshrc that sets fpath,
but it also sets $PS1, which I forgot about.)


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

* Re: allow zsh to source a script and then become interactive
  2017-04-15 15:40         ` Daniel Shahaf
@ 2017-04-15 19:06           ` Anthony Fletcher
  0 siblings, 0 replies; 10+ messages in thread
From: Anthony Fletcher @ 2017-04-15 19:06 UTC (permalink / raw)
  To: zsh-users

Thanks all - I'm happy to have three solutions I can use now. 

Would it be worth actually adding a new, invocation flag? say

        zsh -fi --rcfile $sourcefile

It would be simpler, bash and ksh already have this flag and it would be
useful.

	Anthony.

On 15 Apr 2017 at 15:40:08, Daniel Shahaf wrote:
> Bart Schaefer wrote on Fri, Apr 14, 2017 at 19:45:30 -0700:
> > However, I tried it, and PS1 is imported by zsh built from current git.
> 
> Ah, my bad.  (I normally run Src/zsh with a custom zshrc that sets fpath,
> but it also sets $PS1, which I forgot about.)


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

end of thread, other threads:[~2017-04-15 19:07 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-14 16:55 allow zsh to source a script and then become interactive Anthony Fletcher
2017-04-14 20:28 ` Bart Schaefer
2017-04-14 22:03   ` Anthony Fletcher
2017-04-15  1:00     ` Daniel Shahaf
2017-04-15  2:45       ` Bart Schaefer
2017-04-15 15:40         ` Daniel Shahaf
2017-04-15 19:06           ` Anthony Fletcher
2017-04-14 22:41 ` Nikolay Aleksandrovich Pavlov (ZyX)
2017-04-14 23:14   ` Bart Schaefer
2017-04-14 23:39     ` Anthony Fletcher

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