Skip to main content
deleted 661 characters in body
Source Link
terdon
  • 252.9k
  • 69
  • 481
  • 720

Bash has an option for this:

 --rcfile file Execute commands from file instead of the system wide initial‐ ization file /etc/bash.bashrc and the standard personal initial‐ ization file ~/.bashrc if the shell is interactive (see INVOCA‐ TION below). 

If you have this line in your .bashrc:

export FOO="bar" 

and then run:

bash --rcfile ~/.bashrc -c 'echo $FOO' 

You will get bar printed, as expected. Note the use of single quotes (') as opposed to double ", this saves you from needing to escape the variable's name (you can echo $FOOinstead of echo \$FOO).

Another option would be to set the $BASH_ENV variable:

 When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following com‐ mand were executed: if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi but the value of the PATH variable is not used to search for the file name. 

So, you could do:

BASH_ENV=~/.bashrc && bash -c 'echo $FOO''your_function' 

Bash has an option for this:

 --rcfile file Execute commands from file instead of the system wide initial‐ ization file /etc/bash.bashrc and the standard personal initial‐ ization file ~/.bashrc if the shell is interactive (see INVOCA‐ TION below). 

If you have this line in your .bashrc:

export FOO="bar" 

and then run:

bash --rcfile ~/.bashrc -c 'echo $FOO' 

You will get bar printed, as expected. Note the use of single quotes (') as opposed to double ", this saves you from needing to escape the variable's name (you can echo $FOOinstead of echo \$FOO).

Another option would be to set the $BASH_ENV variable:

 When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following com‐ mand were executed: if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi but the value of the PATH variable is not used to search for the file name. 

So, you could do:

BASH_ENV=~/.bashrc && bash -c 'echo $FOO' 

Another option would be to set the $BASH_ENV variable:

 When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following com‐ mand were executed: if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi but the value of the PATH variable is not used to search for the file name. 

So, you could do:

BASH_ENV=~/.bashrc && bash -c 'your_function' 
Source Link
terdon
  • 252.9k
  • 69
  • 481
  • 720

Bash has an option for this:

 --rcfile file Execute commands from file instead of the system wide initial‐ ization file /etc/bash.bashrc and the standard personal initial‐ ization file ~/.bashrc if the shell is interactive (see INVOCA‐ TION below). 

If you have this line in your .bashrc:

export FOO="bar" 

and then run:

bash --rcfile ~/.bashrc -c 'echo $FOO' 

You will get bar printed, as expected. Note the use of single quotes (') as opposed to double ", this saves you from needing to escape the variable's name (you can echo $FOOinstead of echo \$FOO).

Another option would be to set the $BASH_ENV variable:

 When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following com‐ mand were executed: if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi but the value of the PATH variable is not used to search for the file name. 

So, you could do:

BASH_ENV=~/.bashrc && bash -c 'echo $FOO'