Operating System - HP-UX
1753851 Members
9182 Online
108808 Solutions
New Discussion юеВ

call shell script with . ./myscript.sh

 
SOLVED
Go to solution
Leo The Cat
Regular Advisor

call shell script with . ./myscript.sh

Hi

Why sometimes some scripts needs to be call with the . ./myscript.sh syntax and sometimes with the ./myscript.sh ?

Thanks in advance
Regards
Den
8 REPLIES 8
MarkSyder
Honored Contributor
Solution

Re: call shell script with . ./myscript.sh

The extra . causes the script to run without invoking a new shell.

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
Pete Randall
Outstanding Contributor

Re: call shell script with . ./myscript.sh

And without the extra ., it simply means to look in the current directory ($PWD) to find "myscript.sh.


Pete

Pete
Pete Randall
Outstanding Contributor

Re: call shell script with . ./myscript.sh

That was poorly worded.

In either case, the single period immediately preceding /myscript.sh means to look in the current directory.


Pete

Pete
Leo The Cat
Regular Advisor

Re: call shell script with . ./myscript.sh

Hi

What could be the consequences to launch another shell ?

Regards
Den
Sandman!
Honored Contributor

Re: call shell script with . ./myscript.sh

Read the manpage of sh-posix(1) for details. Relevant portion posted below:

sh-posix(1)
.
.
% . file [arg]...

(period) Read and execute commands from file and return. The
commands are executed in the current shell environment. The
search path specified by PATH is used to find the directory
containing file. If any arguments arg are given, they become the
positional parameters. Otherwise, the positional parameters are
unchanged. The exit status is the exit status of the last
command executed.
James R. Ferguson
Acclaimed Contributor

Re: call shell script with . ./myscript.sh

Hi Den:

The dot-space-filename (whether or not the filename contains a relative path of "./" means that you want to *source* or read the contents of the file into your *current* environment without starting a new shell (and thus losing the variable values).

Sourcing is done to pass environmental variables defined in a file into the environment of another:

# cat mythings
#!/usr/bin/sh
export NAME=Den

# cat someshell
#!/usr/bin/sh
. ./mythings
echo "I am ${NAME}"
exit 0

To see the difference, run 'someshell'; then change the line that reads:

. ./mythings

TO:

./mythings

...and run 'someshell' again.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: call shell script with . ./myscript.sh

Typically scripts are not sourced and need to be executable. Files that are sourced are typically the shell env files (for aliases and functions) and just need to be readable.
Leo The Cat
Regular Advisor

Re: call shell script with . ./myscript.sh

Answer was various and good.