Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am creating a little automated setup script that will modify /etc/profile and $HOME/.profile if some paths are not exported. Then, I'd like to reload these automatically.

I have read that source does this, so I fired up my terminal and entered:

source /etc/profile
source ~/.profile

From the terminal, it didn't output any error.

However, putting these two commands inside the Bash script results in source: not found.

  1. How can I be sure that the script is executed by Bash, even if #!/bin/sh is specified (apparently, it does not guarantee it)?
  2. Why would it say that these two sources cannot be found when they are unmistakably there?
share|improve this question

3 Answers 3

up vote 10 down vote accepted

/bin/sh is not bash. To execute your script with bash, write #!/bin/bash as the first line in your script.

The error source: not found is not dropped, because /etc/profile is not found. It's dropped, because source is not found. source is a Bash built-in function, and you don't execute the script with bash. So it's clear why it is not found. Change the #! line in the script, and it will work.

share|improve this answer
2  
This is what I get trying to write code too late! :) –  Yanick Rochon Jul 30 at 13:32

1. How can I be sure that the script is executed by Bash, even if #!/bin/sh is specified (apparently, it does not guarantee it)?

To be sure that a script written for sh shell (as in your case - see What is difference between #!/bin/sh and #!/bin/bash?) is executed by Bash, just run the following command:

bash script_name

Thus, you will not get that error anymore.

2. Why would it say that these two sources cannot be found when they are unmistakably there?

It doesn't say that those sourced files are not there. It says that the source command is not found. This is normal, because since you start your script with #!/bin/sh line, your script will run using sh and not bash as you may think. Why is it normal? Because source command is a Bash builtin, but not a sh builtin. To source a file in sh, you should use . (dot). Example:

. /etc/profile
. ~/.profile

Another way is to change the shebang line to #!/bin/bash as chaos said in his answer.

share|improve this answer

See this SO question:

/bin/sh is usually some other shell trying to mimic The Shell. Many distributions use /bin/bash for sh, it supports source. On Ubuntu, though, /bin/dash is used which does not support source. If you cannot edit the script, try to change the shell which runs it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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