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?

  • 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). – Fiximan 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
  • 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? – Fiximan Aug 25 '15 at 20:19
10

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

#!/bin/bash
  • Aaaa what a rookie mistake. Thank you very much :) – golobich 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? – golobich 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?) – Fiximan 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 – golobich 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 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.