Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to source additional file when launching bash

Tags:

bash

Bash will source automatic profiles such as .bashrc. --rcfile option can be used to override the automatic script. But I need to source additional personalized file (that's the automatic script plus another file) when launching the bash shell without touching ANY files in $HOME or /etc directory since $HOME directory belongs to application run user. The personalized file must not be located in $HOME directory. Is this possible?

I tried:

    /bin/bash <<EOF
    . /a-directory-outside-of-home/vanilla
    EOF

but it returned to the current shell.

like image 449
techie11 Avatar asked Nov 27 '15 16:11

techie11


People also ask

How do you source a file in shell?

A file is sourced in two ways. One is either writting as source <fileName> or other is writting as . ./<filename> in the command line. When a file is sourced, the code lines are executed as if they were printed on the command line.

Can you use source in a bash script?

The source Command The built-in bash source command reads and executes the content of a file. If the sourced file is a bash script, the overall effect comes down to running it. We may use this command either in a terminal or inside a bash script.

How do you source a file in Linux?

When a file is sourced (by typing either source filename or . filename at the command line), the lines of code in the file are executed as if they were printed at the command line.


2 Answers

bash --rcfile <(cat rcfile1; cat rcfile2)

works just fine and requires no modifications anywhere.

like image 102
Irfy Avatar answered Sep 22 '22 17:09

Irfy


Okay, so you want to run the user's normal .bashrc, followed by your own script, and you want to trigger this behavior in the way that bash is called, correct?

The call:

/bin/bash --rcfile myscript

First line of myscript:

source $HOME/.bashrc
like image 40
Tom Zych Avatar answered Sep 22 '22 17:09

Tom Zych