6

I have file called install.sh and within this file I write something to $HOME/.bashrc file and after that I must call source command. In terminal I can type source $HOME/.bashrc but I can't do this in bash script. If I write this to file, then I get following error:

./install.sh: 1: ./install.sh: source: not found

I am using Ubuntu 12.04 x64.

Any suggestions how to do that?

golobitch
  • 205

2 Answers2

10

If you want to program a bash script, then change your shebang (first line of the script file) to

#!/bin/bash
RSFalcon7
  • 4,407
  • Aaaa what a rookie mistake. Thank you very much :) – golobitch Aug 25 '15 at 19:47
  • Actually here is a problem. If I say source $HOME/.bashrc; in my bash script, then this won't export env variable that I just added to .bashrc file. If I do that in terminal, then it's ok. Any idea why? – golobitch Aug 25 '15 at 20:22
  • the script runs in its own subshell. Try adding some echo $var in the script and it should be fine, just not exported. (or did you actually export the var?) – FelixJN Aug 25 '15 at 20:23
  • In bash script I've added line "export NAME_OF_VAR=/....." to .bashrc and then I want to call source on it. So when I run this script I want to have env variable NAME_OF_VAR set – golobitch Aug 25 '15 at 20:27
  • @golobich You will have the environment variable set in the script. If you're running the script from the command line, that won't set the variable on the command line. To do that, you'd have to source the script on the command line instead of running it as an external command. – Gilles 'SO- stop being evil' Aug 25 '15 at 23:42
0

Perhaps a more simpler way to accomplish what you need, is to use the -f [filename] option provided in bash and load all the environment variable needed from that alternative rc file. The source buliten (built in function) was not meant to function the way you're using it here. The ". , include, and source bulitens were meant to include library (reusable function code) resources into invoked scripts.

bash -l -f /path_to_file/.foo_rcfile