0

In order to run an executable file, I need to run all the commands in the below bash file first, so I just put those commands in the bash file and run the bash file first, but running the bash file doesn't work, I have to run the 4 commands one by one in the terminal and then run my executable file in the same terminal, why do I have to do this, why running the bash file doesn't acheive the same as running the commands one by one:

#!/bin/bash
export LD_LIBRARY_PATH=/repo/$USER/ncmain/out/rcpi/ITC_CXA11466/x86/lib
export ITC_RUNDIR_PATH=/local/scratch/$USER/itc/
export ITC_INSTANCE_NAME=itc_$USER
/local/scratch/ehhozzo/RCS_ROOT/software/itcworld
  • Are you running or sourcing the bash file? If you source it - e.g. $ . bash_file.sh - you will probably get the result you're looking for. What command do you execute to try to run the bash file, and what results do you get? – John Mar 06 '15 at 19:15
  • @John I run the bash file by ./start.bash, I got nothing output, it just sets some env variable. – betteroutthanin Mar 06 '15 at 19:17
  • What is the last command supposed to do? Does it start some sort of software or GUI interface? – John Mar 06 '15 at 19:19
  • @John source works, but why? – betteroutthanin Mar 06 '15 at 19:19

1 Answers1

2

"source works, but why?" - When you run a script that is setting environment variables, you start a subshell by default. The newly set variables don't necessarily come back to your top level shell. When you source a script, you're telling your shell to run the commands in your current shell and not start a subshell, thus the newly set variables will be in the shell you expect them to be in.

John
  • 17,011
  • Excellent & Clear! Can you clear up this thought (too)? Why does Linux allow the dot operator to replace a 'source myscript' (i.e. '. myscript') but yet I cannot run a script inside a pwd with '. myscript' . Instead, we need to do './myscript' ?? What (if any) is the reasoning behind that? – Ricalsin May 26 '15 at 22:33