简体   繁体   中英

Script command losing alias from shell

When I run the script command it loses all the aliases from the existing shell which is not desired for people using lots of aliases. So, I am trying to see if I can automatically source the .profile again to see if it works without the user have to do it.

Here below is the code:

#!/bin/bash -l
rm aliaspipe
mkfifo aliaspipe
bash -c "sleep 1;echo 'source ~/.bash_profile' > aliaspipe ;echo 'alias' > aliaspipe ;echo 'exec 0<&-' > aliaspipe"&
echo "starting script for recording"
script < aliaspipe

Basically I am creating a named pipe and the making the pipe as stdin to the script program, trying to run the source command and then close the stdin from pipe to the terminal stdin so that I can continue with the script.

But when I execute, the script is exiting after I execute "exec 0<&-",

bash-3.2$ exec 0<&-
bash-3.2$ exit

Script done, output file is typescript

Not sure why the exit is called and script is terminated. If I can make the script move the stdin from pipe to terminal then it should be fine.

You can get script to execute a bash login shell by telling it to do so explicitly.

# Gnu script (most Linux distros)
script -c "bash -l"
# BSD script (also Mac OS X)
script typescript bash -l

That will cause your .bash_profile to be sourced.


By the way, redirections are not stacks. When you write exec 0<&- , you're closing standard input, and when bash's standard input is closed, it exits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM