4

whats the difference between executing script like

# ./test

and

# . ./test 

test is simple script for example

#!/bin/bash
export OWNER_NAME="ANGEL 12"
export ALIAS="angelique"

i know the results but im unsure what actually happens

Thanks

phuclv
  • 37,963
  • 15
  • 156
  • 475

3 Answers3

7

./foo executed foo if it's marked as executable and has a proper shebang line (or is an ELF binary). It will be executed in a new process.

. ./foo or . foo loads the script in the current shell. It is equal to source foo

With your example code you need to use the second way if you want the exported variables to be available in your shell.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • why aren't the environment variables not exported in the first scenario .. ? im still confused .. – user1127747 Jan 26 '12 at 12:02
  • @user1127747: See the question [Can a shell script set environment variables of the calling shell?](http://stackoverflow.com/questions/496702/can-a-shell-script-set-environment-variables-of-the-calling-shell) for an answer to that. – DarkDust Jan 26 '12 at 12:07
  • 1
    @user1127747: Because the script runs in a process of its own, it can't export things *back* to its parent's environment. That's just not how environment variables work. With source (the lone dot), you remain in the shell's process, and are thus free to change its environment. – unwind Jan 26 '12 at 12:08
2

With the dot alone, bash is "sourcing" the specified file. It is equivalent to the source builtin and attempts to include and execute the script within the same shell process.

The ./ starts a new process, and the current shell process waits for it to terminate.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
0

The first implies that the script (or binary) be executable. With the script (possibly) containing a shebang line telling which interpreter to use.

The second is a short-hand for "execute [argument] as a shell script". The file passed as argument does not need the executable bit set.

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99