Difference: 'source file.sh' or './file.sh'


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Difference: 'source file.sh' or './file.sh'
# 1  
Old 04-18-2014
Difference: 'source file.sh' or './file.sh'

Dear all,

I would like to know the difference in executing the shell script both ways?
Code:
a.) source file.sh
b.) ./file.sh

I need to initialize some setup which is part of the file.sh. I realized that when I do the
Code:
source file.sh

the setup is all initlaized well. However, with the
Code:
./file.sh

this is not the case.

A bit more of insight as how these commands are different would be really great.

thanking you,
emily
# 2  
Old 04-18-2014
The two are NOT the same...

./script.sh launches the script.sh from the current directory./

However source is identical to . - NOTE the space...

Therefore your alternative is:-
Code:
. ./script.sh

Hope this helps...

EDIT:

From a terminal type:-
Code:
help .

OR...
Code:
help source


Last edited by wisecracker; 04-18-2014 at 06:07 AM.. Reason: See above...
This User Gave Thanks to wisecracker For This Post:
# 3  
Old 04-18-2014
Expanding on what wisecracker said, the commands:
Code:
source file.sh
    and
. file.sh

read commands from file.sh and execute them in the current shell execution environment. When the commands from file.sh are completed, any side effects from running those commands (such as setting shell variables) are still in effect in the current shell.

The command:
Code:
./file.sh

creates a new shell execution environment and runs the commands from file.sh in that new environment. When the commands in file.sh have completed, the new shell execution environment terminates and nothing set in that environment affects the current shell execution environment of the shell that invoked ./file.sh.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 04-22-2014
Thanking you very much, it explain a lot about what I was observing..:-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX utility to find difference in folder, file and contents of file against a base version

Hi, I am trying to find out whether there are any Unix utilities that compares folders, files and contents within the file and provides a comprehensive report. The comparison can be against base version of a folder and file with content. Can you please let me know of such a utility? Thanks,... (6 Replies)
Discussion started by: Sripathi_ks
6 Replies

2. Shell Programming and Scripting

Compare the source definition file with data file

Hi How to compare the source definition file in unix with the data file . Please can you share me example if some one has done it before (3 Replies)
Discussion started by: Raj4fusion
3 Replies

3. UNIX for Dummies Questions & Answers

Match the amount fields in the source file vs trigger file

Hello, I have to write a script to compare the sum of the amount fields in a source file and the amount field in another file. details are: based on the name of the source file (say SALES as an example), a file already available in a path will be grabbed (say SALES_ParmFile) and this file... (4 Replies)
Discussion started by: vijaylak
4 Replies

4. Shell Programming and Scripting

Split File by Pattern with File Names in Source File... Awk?

Hi all, I'm pretty new to Shell scripting and I need some help to split a source text file into multiple files. The source has a row with pattern where the file needs to be split, and the pattern row also contains the file name of the destination for that specific piece. Here is an example: ... (2 Replies)
Discussion started by: cul8er
2 Replies

5. Programming

how to copy downloaded file into my source file folder (putty/unix)

I need to "Ensure that when you download libchat.a from the VLE you have copied it to the same folder on ius as your source files. You then refer to the library (and the libraries it needs) with: gcc -o outputfile sourcefile.c -L. -lchat -lsocket -lnsl" But I have no idea what this means! (I... (2 Replies)
Discussion started by: fakuse
2 Replies

6. Shell Programming and Scripting

rsync - update file on backup when file renamed on source

hi all, Please help me with rsync. I configured rsync to preserve timestamps using the -a option. When i renamed fileA to fileB on source machine I have to copies at the backup server. The aim is to keep the most recent file. fileA & fileB has same contents. When i renamed fileB to... (2 Replies)
Discussion started by: coolatt
2 Replies

7. Shell Programming and Scripting

extracting columns using pattern file from source file

Hi experts,Please help me for the below requirement. i have a source file.(lets say contains 50 columns). I am extarcting five columns from the source file by using pattern file. for example input file:--------a,b,c,d,"a,g","v b",s,koutputfile=======a,"a,g","v b",s,kThanks in advance subhendu (2 Replies)
Discussion started by: subhendu81
2 Replies

8. Shell Programming and Scripting

use of format file to extract columns from a source file

hi experts lets say my format file is B B ========= column no,name,type,length 1,ee,N,12 3,hj,N.4 4,kl,N,5 source file ======== d e f g h i 5 8 9 7 6 5 1 3 4 5 6 6 (2 Replies)
Discussion started by: subhendu81
2 Replies

9. Shell Programming and Scripting

Post Shell programming: Question about source a file and read data from the file

This is shell programming assignment. It needs to create a file called .std_dbrc contains STD_DBROOT=${HOME}/class/2031/Assgn3/STD_DB (which includes all my simple database files) and I am gonna use this .std_dbrc in my script file (read the data from the database files) like this: .... (3 Replies)
Discussion started by: ccwq
3 Replies

10. Shell Programming and Scripting

Get file from source server and delete the ftp file

Hi there Everyone, I need some help/information/advise for the following questions: I'm writing the following script: #Beginning of the script ftp -n <source server> > ${log_dir}/test_get.log << END user <user_name>@<IP Address> <passwd> verbose bin !echo "'List Files Before Getting... (3 Replies)
Discussion started by: lweegp
3 Replies
Login or Register to Ask a Question