views:

1106

answers:

4

The UNIX "/usr/bin/script" command will create a running transcript of your shell session (see "man script" for more info).

However, when inside a script instance, it seems to forget the parent shell's env vars, aliases, etc.

The following example demonstrates how the "ll" alias I define is ignored inside "script":

zsh> mkdir temp
zsh> cd temp

zsh> alias "ll=ls -alF"

zsh> ll
total 24
drwxr-xr-x   2 me   mygroup    4096 Feb 18 13:32 ./
drwxr-xr-x  28 me   mygroup    8192 Feb 18 13:32 ../

zsh> script a.out
Script started, file is a.out

$ ll

zsh: command not found: ll
$ exit
Script done, file is a.out

zsh> ll
total 32
drwxr-xr-x   2 me   mygroup    4096 Feb 18 13:32 ./
drwxr-xr-x  28 me   mygroup    8192 Feb 18 13:32 ../
-rw-r--r--   1 me   mygroup     182 Feb 18 13:32 a.out

So, how can I get the "script" process to inherit the env settings from the parent shell?

[EDIT:] Okay, env vars are not forgotten. Just the aliases. Re-sourcing the .profile or something would work... but how can I make that happen automatically?

+1  A: 

An alias isn't an environment variable. You could source your .profile or where ever you set up the alias. Also take a look at the $SHELL environment variable.

The script command isn't terribly complicated. It wouldn't be too difficult to replicate and make it work the way you expect.

Jon Ericson
Yah, I did try to say "env settings" and all.Having to re-source the .profile would be okay if you can 'splain how to make it happen automatically upon invocation of /usr/bin/script. Don't want to force the user to remind himself.
robmandu
A: 

As noted by Jon Ericson, aliases are not part of the environment. You will find all your environment in your script.

To get all your aliases in the script, you can save them in a file then reload them:

    $ alias myls="ls -lCF"
    $ alias -L >/tmp/alias.zsh
    $ script
    $ . /tmp/alias.zsh
    $ myls

If you put your aliases in a file called .zshrc in your home directory it will automatically be loaded.

kmkaplan
Okay... that's fine. I understand how to source the .profile or an personal alias script you show manually. But what I really want is to see that source action happen *automatically* without bugging the user.
robmandu
And yes, while .zshrc is indeed automatically loaded at login, it is *not* loaded by the /usr/bin/script. Just because I manually defined "ll" in my example for clarity doesn't mean I'm not aware of how to establish per-user config files.
robmandu
+1  A: 

It works OK when I start it under bash. Maybe there's something in your zsh configuration that's mucking it up, or it's not sourcing your zsh's startup files. You could try: script -c zsh

Which may force it to start a new zsh shell and have it source your zsh config files.

Mick T
Using -c switch fails with help msg... usage: script [ -a ] [ typescript ]
robmandu
Huh... switched to bash shell, defined alias, invoked /usr/bin/script, and tried to use alias. It failed with error message, "zsh: command not found: ll"So, apparently script forks off using the shell defined in passwd, but it won't source the user's local config file??? (for zsh, at least)
robmandu
Agreed, on another box where I have bash defined as default shell, aliases are inherited into /usr/bin/script. So... the problem I see is unique to zsh (and maybe others?)... but bash works okay.
robmandu
Having ksh defined as user's default shell in passwd also works correctly. Seems like this must be a deficiency in zsh or simply a config option. Hmmmmmmm.
robmandu
A: 

Alias command with examples