Wednesday, November 25, 2009

Dot (.) in linux and shell script

- Dot(.) is equivalent the source command in shell script and linux command line. From command line we can execute a shell script using dot (.). Within a script, a "source file_name" or ". file_name" loads the file file_name. The command "source" or "." just imports code into the script, appending to the script (it is same like include command in php or #include in c). The net result is the same as if the "sourced" lines of code were physically present in the body of the script. This technique is useful in situations when multiple scripts use a common data file or function library.

Let's assume that my welcome.sh is like below which later included using . within main.sh file.

$ cat >welcome.sh
echo "Welcome to shell script programing"

$ cat >main.sh
. welcome.sh
echo "This is main file"

$ sh main.sh
Welcome to shell script programing
This is main file


- Dot (.) is the part of the hidden file name. A leading dot is the prefix of a "hidden" file.

$ ls
main.sh  newfile  semicolon_test.sh  welcome.sh

$ touch .hidden_file

$ ls
main.sh  newfile  semicolon_test.sh  welcome.sh

$ ls -a
.  ..  .hidden_file  main.sh  newfile  semicolon_test.sh  welcome.sh


In the example ls -a shows the hidden file .hidden_file

- When working with directory, a single dot (.) represents the current working directory, and two dots (..) represent the parent directory.
Example:

$ pwd
/home/test/t_dir

$ cd .

$ pwd
/home/test/t_dir

$ cd ..

$ pwd
/home/test


- While copying files we can use dot (.) to represent current directory.
The following example will copy the file /home/test/f to present working directory.

$ cp /home/test/f .

Thanks & Regards,
Anto Joe Natesh I

No comments: