0

In my script, I try to source two files to fetch the variables.

But it failed to get the variables defined in ~/.bashrc.

OS: Ubuntu Desktop 20.04.2 LTS
$ cat debug.sh

#!/usr/bin/env bash

cat > ~/env.sh << EOF
VAR1="123"
EOF

echo "VAR2=456" >> ~/.bashrc

source ~/env.sh
source ~/.bashrc

set -u
echo ${VAR1}
echo ${VAR2}
$ ./debug.sh

123
./debug.sh: line 14: VAR2: unbound variable
4
  • 1
    What's in ~/.bashrc? Commented Jul 17, 2021 at 4:04
  • Why not just have your shell play Russian Roulette in a game of "Will I start next time?" (dynamic modification of .bashrc -- bad idea...) Use a temporary file instead. Commented Jul 17, 2021 at 6:29
  • you might give a try replacing source with . to source the file inside the script...example: . ~/.env.sh Commented Jul 17, 2021 at 7:11
  • Don't reuse .bashrc as a common library. Its purpose is to configure an interactive shell. If there is code you want in both a script and in an interactive shell, put that in a 3rd file and source it from both .bashrc and your script. Commented Jul 18, 2021 at 14:11

1 Answer 1

0

In the ubuntu version, .bashrc will check whether it is running interactively, if it is not, it will do nothing. Non-interactive means you called it inside another shell, which you can just comment out the following lines in .bashrc.

# If not running interactively, don't do anything

case $- in
*i*) ;;
  *) return;;
esac
Sign up to request clarification or add additional context in comments.

1 Comment

That check is there for a reason. Don't comment the lines out just so you can force .bashrc to be used for a purpose it isn't intended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.