Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to export bash shell options to subshell?

Tags:

bash

shell

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
like image 735
Michael Brux Avatar asked Jun 11 '14 22:06

Michael Brux


People also ask

How do you make a variable available to Subshells?

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.

How do I export shell variables?

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.

How do you set an environment variable that is accessible from subshell?

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.

What is Bash export command?

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.


1 Answers

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.

like image 126
Jadzia Avatar answered Nov 12 '22 23:11

Jadzia