LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 03-01-2013, 01:09 PM   #1
js82
LQ Newbie
 
Registered: Mar 2013
Posts: 4

Rep: Reputation: Disabled
Question about . ./somescript.sh


I understand that if you have a particular file ('hello.sh') that you want to execute, you need to refer to it with absolute path: "./hello.sh". What is the difference between "./hello.sh" and ". ./hello.sh"?
 
Old 03-01-2013, 01:34 PM   #2
js82
LQ Newbie
 
Registered: Mar 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Is it that the second method actually sources that file instead of executing it?
 
Old 03-01-2013, 01:38 PM   #3
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
"./hello.sh" isn't the absolute path: "/home/js82/hello.sh" would be an absolute path.

"./hello.sh" executes a file called hello.sh in the current directory, while ". ./hello.sh" sources hello.sh (running it as if you typed each line into the command line)
 
Old 03-01-2013, 01:39 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 2,239

Rep: Reputation: 577Reputation: 577Reputation: 577Reputation: 577Reputation: 577Reputation: 577
First, there is a bit about what an "absolute path", and "relative path"; followed by a short reference on quoted strings as interpreted by command interpreters.

An absolute path always starts with a "/", as it is specifying the full path starting from the root of the directory tree. A relative path may be a "." type prefix - such as "../name" is a relative path, as is "./name/name2", and "name/name2".

Relative paths are based on the current working directory which is essentially prefixed on the specification. If the current working directory is /home/user, then "../name" has a prefix (/home/user") turning it into an absolute path /home/user/../name.

If you look in any directory (use "ls -a") you will see (at a minimum) of "." and "..". The "." is a name connected to the current directory, the ".." is a name connected to the parent directory of the current directory. This makes the absolute path "/home/user/../name" equivalent to "/home/name". In the case of "./name/name2" becomes "/home/user/./name/name2" which is equivalent to "/home/user/name/name2". In both of these cases the path requires an extra lookup for the ".." or ".".

Now, quotes are/can be handled differently by different shells, but the convention is to use quotes to create lists. The default has the list created from the command line separated by white space characters (spaces and/or tabs), plus various special characters that may be used. As the list is created, the quote characters get removed because the value to be used is what is quoted, but not the quote characters themselves. So a command "ls" is the same as ls (without the quotes). The quotes only become significant when they enclose a white space character (blank or tab).

If the file containing the "hello world" script was NAMED "hello world" (with the space), then you can't just use the file name to be executed- it would split the name into a list of two words, hello, and the word world. Then the interpreter would attempt to find the executable named "hello"... and likely fail. In this case the command you would have to give is "hello world" (with the quotes) before it would work.

Next, a bit about how commands are looked for...

IF a command (the first string on a command line) starts with a "/", "./","../" then the path to the executable is used, either as an absolute path (the /), or based on the current working directory. If the command starts with any other character (not a special character), the command interpreter is supposed to split up the "PATH" environment variable into a list (split on each ":" character found). Each entry on the list (starting from the first, and with a "/" appended to the entry) is then used as a prefix to the string. If the full path so created doesn't exist, then it goes to the next entry.
If the file does exist (and is marked executable) then that file gets executed.

The string list created from the command line is passed to the executable as parameters. The first parameter is the name of the file executed (for a shell script this is $0), the second string identified is $1... The parameter list can be terminated several ways: a ";" character is treaded as if it were an end-of-line, shell special characters can terminate the list to allow for input/output/error output to be redirected, a "|" (pipe) symbol is used to identify when the output of the current command is to be connected to the input of a following command.
 
1 members found this post helpful.
Old 03-01-2013, 01:42 PM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
./hello.sh means you're executing an executable file or invoking it. It's a way to execute some script using shell interpreter mentioned in the file. It's similar to:
Code:
~$ sh ./hello.sh
~$ sh hello.sh
. ./hello.sh means you're sourcing it line by line by reading it's each line and loading what's mentioned in that line. It's not a way to execute a script, but a way to load all executables mentioned inside hello.sh. It's similar to:
Code:
~$ source ./hello.sh
OR
~$ source hello.sh

Last edited by shivaa; 03-01-2013 at 01:44 PM.
 
Old 03-01-2013, 01:42 PM   #6
js82
LQ Newbie
 
Registered: Mar 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Snark1994 View Post
"./hello.sh" isn't the absolute path: "/home/js82/hello.sh" would be an absolute path.

"./hello.sh" executes a file called hello.sh in the current directory, while ". ./hello.sh" sources hello.sh (running it as if you typed each line into the command line)

Thanks for the info, that's sort of what I figured but wasn't sure. What would be the reason for sourcing the file instead of just executing it? I also mistyped about the absolute path, I meant relative.
 
Old 03-01-2013, 02:54 PM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 2,239

Rep: Reputation: 577Reputation: 577Reputation: 577Reputation: 577Reputation: 577Reputation: 577
sourcing a file is used when the current shell environment is being updated.

It is a bit difficult to give simple examples, but one would be adding directories to the PATH environment variable. If you just execute it, the PATH environment variable would be modified - but only within the environment of the executed script. Sourcing the script would execute the commands within the script as if they were typed in - thus modifying the environment of the running shell.
 
Old 03-02-2013, 06:55 AM   #8
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Quote:
Originally Posted by jpollard View Post
sourcing a file is used when the current shell environment is being updated.

It is a bit difficult to give simple examples, but one would be adding directories to the PATH environment variable. If you just execute it, the PATH environment variable would be modified - but only within the environment of the executed script. Sourcing the script would execute the commands within the script as if they were typed in - thus modifying the environment of the running shell.
Exactly as jpollard says: another example would be a script to change your prompt (e.g. maybe if you're working with SSH you want a reminder of your current hostname, so you know if you're SSH'd into a machine or not - but you don't want the cluttered prompt most of the time). The only way such a script could work would be if you sourced it rather than executed it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] GPL question (Version 2, June 1991) - physical media availability question LicenseQuestions Linux - Newbie 1 12-01-2012 06:34 PM
basic html question - download link to files on my webpage question Davno Linux - Server 5 12-25-2009 07:24 AM
linux distro question & mysql install question natalie.aloi Linux - Newbie 5 07-19-2009 08:28 PM
what happens when a script is run using 'sh <somescript>'? ananthbv Programming 16 09-16-2006 08:59 PM
Question, Apples Contribution to Open Source + MacOs file structure question Higgy3k Other *NIX 5 07-25-2005 04:23 AM


All times are GMT -5. The time now is 02:46 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration