2

To execute a script we type ./script.ksh but to connect to a database first we go the below path by typing . /

$ . /home/sqllib/db2profile 
$  db2

why should we use . / before home ?

  • 1
    Please edit your question and clarify whether you are running . /home/sqllib/db2profile (a space between the . and the /) or ./home/sqllib/db2profile (no space). The two are different commands. – terdon Feb 28 '16 at 17:14
  • Can you please let me the difference between using . (space) / vs ./ – star Feb 28 '16 at 17:44
  • 1
    a .(space) is the same as the source command. A ./ is part of a path to a file or directory which means "here" (this directory, or just the pwd). – user79743 Feb 28 '16 at 17:51
  • @star Celada's answer does this. The first big paragraph is about using . in a pathname, the second is about using it as a command – Michael Mrozek Feb 28 '16 at 17:52
8

The . that you find as part of pathnames has nothing to do with the . command.

. is a special file name that exists in each directory and designates the current directory. So ./script.ksh designates the file script.ksh in the current directory and so does ././script.ksh and so does ./././script.ksh (it doesn't matter how many times you ask the pathname resolver to go from the current directory to itself to itself to itself...). Among other reasons, we use ./script.ksh instead of simply script.ksh to make it clear that we mean a pathname, not a shell command.

. is also a shell builtin that makes the shell source the contents of the file given to it as an argument into the shell's current environment, as opposed to executing it as an external command. The . command can also spelled source in many shells (this is a compatibility alias that comes from csh heritage). Notice that as this is a shell command (that consists of a single punctutation character, like :), it is always followed by whitespace.

0

In this case the . tells the shell to execute the following script (in this case /home/sqllib/db2profile) in the context of the current shell process instead of starting up a new shell process to run the script.

The reason for doing this is that the script whose name follows the . probably creates environment variables which will be needed by other programs. Without the . in front of the shell script name a new process would be started to run the script, and thus any environment variables created by the script would be lost when the script completed and its process terminated. By putting the . before the script, the script is run by the current shell process so that any environment variables which are created by the script are created in the context of the current shell, and thus these environment variables are available to scripts and programs which are run later, such as the db2 command on the next line.

  • Hi Bob thanks for the information kindly enlighten me on in-depth on shell concept what do you mean by current shell ? – star Dec 2 '16 at 17:35
  • Normally when a command is executed the shell starts a new process for the command to run in. This means that any environment changes made by the command are made in the new process, and are thus lost when the process in which the command is running terminates. Preceding a command with . tells the shell to run the command in the same process the shell is running in, so that any changes made by the command are made in the process the shell is running in, and are thus preserved when the command terminates. – Bob Jarvis Dec 2 '16 at 17:42
  • @ Bob thank you . 1 last query . shell run in a separate process .when i execute a command it runs under new process . so there are 2 process involved here but using . will run the command under the shell process instead of creating a new process is that correct ? – star Dec 2 '16 at 17:52
  • Yes, that's correct. – Bob Jarvis Dec 2 '16 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.