Differences between revisions 18 and 20 (spanning 2 versions)
Revision 18 as of 2016-08-29 12:57:12
Size: 1077
Editor: GreyCat
Comment: Dear Linux people: not everyone is on Linux. Sincerely, the rest of the world.
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 15: Line 15:
Or, if you happen to be on Linux: Or:
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)