Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was just reading up on how Unix shells work (bash in particular), and I tried something that didn't make sense to me.

As I understand it, the source command runs the program you give it in the current shell process instead of forking a child process.

On the other hand, running a command followed by & returns control to the user before the process that runs finishes. If you run a command without source but with &, the current shell returns control to the user before the child process exits.

But when I created a file called test.txt containing hello world, and ran:

source /bin/cat test.txt

… I got -bash: ????: command not found.

Similarly, when I tried to load my virtualenv using

source ./venv/bin/activate &

I just got the exit status and process id [1] 26489

What's going on? Particularly for the second command. I’m confused.

How does source work, and what happens when you use it with &?

share|improve this question
source file or . file (for any Bourne compatible shell) will read commands from file without starting a new process. – Henk Langeveld Aug 13 '12 at 19:16
up vote 4 down vote accepted

source loads the bash script file and interprets it in the current shell environment. cat is not a bash script but a binary program file and bash cannot successfully interpret its contents as script code. Besides that, bash scripts not designed for use with source will possibly misbehave.

source filename [arguments]

Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename. [...] If any arguments are supplied, they become the positional parameters when filename is executed. Otherwise the positional parameters are unchanged. The return status is the status of the last command exited within the script (0 if no commands are executed), and false if filename is not found or cannot be read.

When you run it with &, is will execute in the background. Any change to status will only be displayed when the prompt is printed the next time, and it looks like this:

[1]+  Done                    source .bashrc

If you really want to launch a program that replaces the shell, take a look at exec.

   exec [-cl] [-a name] [command [arguments]]
          If command is specified, it replaces the shell.  No new  process
          is  created.  The arguments become the arguments to command.  [...]
share|improve this answer
thanks, that's very helpful. I assume that source run with & doesn't affect the current shell process. (e.g., source ~/.bashrc & doesn't reload your bashrc file)? – karansag Aug 15 '12 at 22:23
@karansag Apparently, it runs in the same process, but as a background job that does not affect your running session. cd & does not change your working directory either. – Daniel Beck Aug 15 '12 at 22:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .