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
  • 1
  • 3
  • 6
  • Is your `#!` line `#!/bin/sh` ? or `#!/bin/bash` ? – Sergiy Kolodyazhnyy Aug 25 '15 at 19:38
  • 1
    I think that `sh` and `bash` support `.` as a name for `source` eg `. ./install.sh` – ctrl-alt-delor Aug 25 '15 at 19:43
  • 2
    Only `.` is POSIX, so the `source` might be the problem here (if it is not run under `BASH` but rather `SH`). – FelixJN Aug 25 '15 at 19:45
  • In Unix, we do not give executables file-extensions: Consider that you call you script from another script, and then re-write it in python (other languages are available). What should you now call the first script? What happens to the behaviour of the 2nd script? – ctrl-alt-delor Aug 25 '15 at 19:59
  • @Fiximan your comment does not make sense. I can not determine if you are saying one thing, the opposite, or something else. – ctrl-alt-delor Aug 25 '15 at 20:00
  • 1
    @richard using `.` is POSIX compliant, while `source` is not. So in case he tries to source the file via the `source` command but is doing so with `SH`, he will fail. Did that clarify it? – FelixJN Aug 25 '15 at 20:19
  • @fiximan In english grammar the word before `but` should be followed by a comma (`,`). The phrase before the comma, should may sense on its own: be a valid sentence. – ctrl-alt-delor Aug 25 '15 at 21:33
  • You should not read `.bashrc` from a script. `.bashrc` is meant to be executed only in interactive environments; it's for setting up terminal parameters, prompts, completion, etc. [Don't set environment variables in `.bashrc`](http://unix.stackexchange.com/questions/3052/alternative-to-bashrc/3085#3085). [What are you really trying to achieve?](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Gilles 'SO- stop being evil' Aug 25 '15 at 23:44

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,367
  • 6
  • 30
  • 56
  • 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