Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excute magic diffrence between ". ./script" "./script"? 1

Status
Not open for further replies.

StraightL

MIS
Jun 7, 2004
13
AU
It is really weird,i just want to set an variable.

#. ./script can but #./script can't

look.............

fargo@Sarge:~$ ls -l script
-rwxr-xr-x 1 fargo fargo 16 2005-05-19 09:10 script

fargo@Sarge:~$ cat script
export XXY=/usr

fargo@Sarge:~$ ./script
fargo@Sarge:~$ echo $XXY

fargo@Sarge:~$ . ./script
fargo@Sarge:~$ echo $XXY
/usr

fargo@Sarge:~$ echo $SHELL
/bin/bash

 
. ./script works as if you typed the commands in at the command line, so whatever is set in the script remains intact in your shell window.

./script only sets the variable for the shell in which the script runs. You don't really want your scripts to affect your shell environment.

If you want to set variables for your login shell environment, then you should add them to your .bash_profile so they get set when you login or execute a command like "/bin/bash --login -i".

 
Alternately, you can use the export command in your script. i.e. export XXY=/usr. You can later remove the variable unset. i.e. unset XXY.


--== Anything can go wrong. It's just a matter of how far wrong it will go till people think its right. ==--
 
export from a subshell won't work to change the parent shell, you have to source the file with "." or "source" depending on your shell. Export only makes variables available to subshells of the current shell.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top