Inheriting aliases inside UNIX /usr/bin/script

2019-06-27 06:27发布

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?

4条回答
神经病院院长
2楼-- · 2019-06-27 06:35

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.

查看更多
手持菜刀,她持情操
3楼-- · 2019-06-27 06:37

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.

查看更多
ら.Afraid
4楼-- · 2019-06-27 06:40

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.

查看更多
男人必须洒脱
5楼-- · 2019-06-27 06:53

For the specific case of an alias for zsh ... if you place the alias(es) in .zshenv (rather than .zshrc) it should be executed on your behalf automatically.

I know this is an old thread, but sometimes people read old threads ;>

alias foo='print FOO'
[khb@vm]~% foo
FOO
[khb@vm]~% script
Script started, file is typescript
[khb@vm]~% foo
zsh: command not found: foo
zsh: use 'exit' to exit.
[khb@vm]~% exit
Script done, file is typescript
[khb@vm]~% mv .bork .zshenv
[khb@vm]~% foo
FOO
[khb@vm]~% script
Script started, file is typescript
[khb@vm]~% foo
FOO
[khb@vm]~% 
 zsh: use 'exit' to exit.
Script done, file is typescript
[khb@vm]~% 
查看更多
登录 后发表回答