Difference in Executing a Script

Can anyone tell me the difference between the following
execution ways:

1) . ./filename

2) ./filename

Also when to use the either.

./filname means execute the file from current directory

../filename means execute file from parent directory

Regards,
Vangalli

How about this

. filename and how does it differ from ./filename

no dear............

./ is used to execute the files which dont have global execution permission.......... once u add this file to /usr/bin and shnage the permission to executable .... it wont work................

pretty confused

I have created a file abc .The file permissions are -rw-r--r-- .
Now when i execute using
./abc ---> it gives cannot execute

But if i execute using . ./abc OR . abc
It executes

Could you please elaborate on this.

"./file" means execute the file located in the current directory. "./" is a realtive path where "." means "actual directory".
". ./file" does not exactly execute the file, it's like an "import" of its contents, so, if inside the file there are exports or shell commands, they will be executed.
Regards.

That's correct, to execute the file like "./abc" you need to give it execution permissions, for instance: "rwxr-xr-x"

". abc" works bacause you have "." in your PATH, which is not a good idea... :slight_smile:

Shivdatta,

Not to confuse the issue... But this is my understanding.

1) The dot with a space tells the shell to source the file. The dot slash tells the shell the file is in the current directory.
Example. You have two script files. One called "run" the other called funct.

Your funct file would look something like this.

lsd ()
{ ls -al $1}

Your run file would look something like this.

#! /bin/ksh
. /path-to-file/funct
programming stuff
lsd some-file

When you execute run it will source the fuctions into the current shell. Then you simply call the function from within your script. This is most useful for common functions used in many scripts. You need only type the functions in once when creating the funct file. Then you source in the functions in any other script you need them in. This can also be used from the command line to source functions into your current shell. You can then use these fuctions as if they were commands.

2) Simply tells the shell to execute the file in the current directory. Mostly used when the directory your in is not in your PATH, or there are mutiple prgrams with the same name (Bad Idea) and you want to run the instance in the current directory.

Hope this helps.

1)./ means you have file in your current directory and from where you want to use.

2).<space>filename means you want to run the file in the current shell.The . command executes a script without using a subshell.it also doesn't require the script to have executable permission.

the use of this is to keep some function in file and run that file as
. filename then u can use those functions in a command line directly as you use commands.

one more use is to modify .profile file and you want to reflect changes without rebooting run the command
.<space> .profile