tags:

views:

154

answers:

4

This question may sounds silly to you. Today I was following some instructions to install a software in Linux. There was a script that needs to be run first. It set some environment variables.

The instruction told me to execute . ./setup.sh, but I made a mistake by executing ./setup.sh. So the env was not set. Finally I noticed this and proceeded.

I want to know what exactly is the difference between both? I am completely new to Linux so please be as elaborate as possible.

A: 

"." refers to the current directory. So "./script.sh" means run the script in the current directory. "../script.sh" would run script.sh in the parent directory. ". ./script.sh" (with a space between the dots) would complain in some shells like csh but in bash ". foo" is shorthand for "source foo".

dreeves
`.` is a shell built-in command. See leeeroy's answer.
Dave Hinton
Just figured that out. I tried it in csh and it said "command not found". I see now that in bash it's a shortcut for "source".
dreeves
`.` is a valid bash command - equivalent to `source`
David Gelhar
+2  A: 

Edit: I just realized I didn't answer the other part of your question .. but the answer by leeroy does that. I kind of answered something else, but I hope it helps :-)

The sh function runs bash on the script you present it with. See the man page for more info, but you can see sh is basically the synonym for bash

When you run a script ala ./setup.sh it identifies the script based on what is at the top of the file, normally referred to as the "Shebang"

A bash script would have

#!/bin/sh

Or similar at the top of the file, allowing you to use the dot method. You can also use other things, like a Python script can have

#!/usr/bin/env/python

And if your path is correct, it would run the script as a Python script instead of a bash one using the dot notation.

Hope that explains it in a simple manner!

Bartek
That's all true, but it does not answer the question. The key point is that `. ./setup.sh` can alter the current shell, and `./setup.sh` cannot.
David Gelhar
+12  A: 

./setup.sh runs the script, a new shell will be started that runs the script. That new shell cannot affect the parent shell that started the script.

. ./setup.sh is shorthand for source ./setup.sh and it will run the script in the current shell, not start a new one to run the script. This means the script can alter the behavior of the current shell, e.g. set new environment variables.

leeeroy
A: 

Just run "source /path/to/setup.sh"

This will set up environment variables in current shell.

greatboy