5

I am trying to source a bash script inside a bash -c "..." command but it doesn't work.

If I run the command outside of the bash -c "..." it works.

I need to use bash -c "..." because I want to ensure people are using Bash (no sh, tcsh, etc.)

working:

bash> source /neuro/arch/scripts/neuro-fs stable;echo $XAPPLRESDIR;
/neuro/arch/x86_64-Linux/packages/matlab/MATLAB_Compiler_Runtime/current/X11/app-default

not working:

bash> bash -c "source /neuro/arch/scripts/neuro-fs stable;echo $XAPPLRESDIR;"

In the Bash script, we define XAPPLRESDIR as follows:

export XAPPLRESDIR=/neuro/arch/x86_64-Linux/packages/matlab/MATLAB_Compiler_Runtime/current/X11/app-defaults

#!/bin/bash at the beginning of the script or not doesn't seem to make any difference.

Is export a "bad" practice in this case?

1 Answer 1

3

The variable expansion in the "bash -c" is happening before "bash -c" is run; you need to do something like:

bash -c "source /neuro/arch/scripts/neuro-fs stable;echo \$XAPPLRESDIR;"
Sign up to request clarification or add additional context in comments.

Comments

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.