I set bash options (e.g. shopt -e autocd) in current shell & want to export/copy this options to a newly created subshell. Is there a way to do this?
Bash Version is: GNU bash, Version 4.3.11(1)-release (x86_64-pc-linux-gnu)
Example:
$shopt -s autocd
$shopt autocd
autocd on
$bash
$shopt autocd
autocd off
To make a variable available in a subshell (or any other subprogram of the shell), we have to “export” the variable with the export command. Try the previous serie of commands again, but do export fruit before starting the subshell.
You can use the export command to make local variables global. To make your local shell variables global automatically, export them in your . profile file. Note: Variables can be exported down to child shells but not exported up to parent shells.
The easiest way to set environment variables is to use the export command. Using export, your environment variable will be set for the current shell session. As a consequence, if you open another shell or if you restart your system, your environment variable won't be accessible anymore.
Export is a built-in command of the Bash shell. It is used to mark variables and functions to be passed to child processes. Basically, a variable will be included in child process environments without affecting other environments.
You can export many shell options analogous to shell variables by using the corresponding shell variable BASHOPTS which stores the shell options:
export BASHOPTS
Here is some information of the Bash man page:
BASHOPTS: A colon-separated list of enabled shell options. Each word in the list is a valid argument for the -s option to the shopt builtin command (see SHELL BUILTIN COMMANDS below). The options appearing in BASHOPTS are those reported as on by shopt. If this variable is in the environment when bash starts up, each shell option in the list will be enabled before reading any startup files. This variable is read-only.
There is a closely related variable SHELLOPTS, but this only seems to work well if bash is invoked directly (e.g. via bash) rather than via sh because sh enables the POSIX mode of Bash. Here is more information on this issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With