Differences between revisions 19 and 20
Revision 19 as of 2016-08-31 00:04:54
Size: 1047
Editor: ormaaj
Comment: Eh? /dev/fd was not a Linux invention and is widely available. Procsub's _only_ advantage is the fifo fallback (and only 3 shells support it). Also IMO it's better to pass a seekable file.
Revision 20 as of 2016-08-31 00:32:52
Size: 1539
Editor: ormaaj
Comment: Portability nazis shouldn't use --rcfile either.
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
Just specify a different start-up file: When starting bash in non-POSIX mode, specify a different start-up file with `--rcfile`:
Line 18: Line 18:
 ~ $ bash --rcfile /dev/fd/3 -i 3<<<'cowsay moo'  ~ $ bash --rcfile /dev/fd/9 -i 9<<<'cowsay moo'
Line 31: Line 31:
Variant question: ''I have a script that sets up an environment, and I want to give the user control at the end of it.'' For POSIX-compatible shells, use the `ENV` environment variable:
Line 33: Line 33:
Put {{{exec bash}}} at the end of it to launch an interactive shell. This shell will inherit the environment (which does ''not'' include aliases, but that's OK, because [[BashFAQ/080|aliases suck]]). Of course, you must also make sure that your script runs in a terminal -- otherwise, you must create one, for example, by using {{{exec xterm -e bash}}}. {{{
  ~ $ ( { ENV=/dev/fd/9 exec -a sh bash -i; } 9<<<'echo yo' )
yo
+sh-4.3$ exit
exit
}}}

Unfortunately, ENV only works in bash and zsh when executed in their respective POSIX modes. Confusingly, Bash also has BASH_ENV, which only works in non-posix mode, and only in non-interactive shells.

=== Variant question: ''I have a script that sets up an environment, and I want to give the user control at the end of it.'' ===

Put {{{exec bash}}} at the end of it to launch an interactive shell. This shell will inherit the environment variables and open FDs but none of the shell's internal state such as functions or aliases, since the shell process is being replaced by a new instance. Of course, you must also make sure that your script runs in a terminal -- otherwise, you must create one, for example, by using {{{exec xterm -e bash}}}.

I want to launch an interactive shell that has special aliases and functions, not the ones in the user's ~/.bashrc.

When starting bash in non-POSIX mode, specify a different start-up file with --rcfile:

bash --rcfile /my/custom/bashrc

Or:

bash --rcfile <(printf %s 'my; commands; here')

Or:

 ~ $ bash --rcfile /dev/fd/9 -i 9<<<'cowsay moo'
 _____
< moo >
 -----
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
+bash-4.3$ exit
exit

For POSIX-compatible shells, use the ENV environment variable:

  ~ $ ( { ENV=/dev/fd/9 exec -a sh bash -i; } 9<<<'echo yo' )
yo
+sh-4.3$ exit
exit

Unfortunately, ENV only works in bash and zsh when executed in their respective POSIX modes. Confusingly, Bash also has BASH_ENV, which only works in non-posix mode, and only in non-interactive shells.

Variant question: ''I have a script that sets up an environment, and I want to give the user control at the end of it.''

Put exec bash at the end of it to launch an interactive shell. This shell will inherit the environment variables and open FDs but none of the shell's internal state such as functions or aliases, since the shell process is being replaced by a new instance. Of course, you must also make sure that your script runs in a terminal -- otherwise, you must create one, for example, by using exec xterm -e bash.


CategoryShell

BashFAQ/023 (last edited 2016-08-31 00:32:52 by ormaaj)