Friday, October 2, 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/Arju/t_dir

$ cd .

$ pwd
/home/Arju/t_dir

$ cd ..

$ pwd
/home/Arju


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

$ cp /home/Arju/f .


- In case of regular expression while matching characters, a "dot" matches a single character. About dot (.) it is discussed in http://arjudba.blogspot.com/2009/07/list-of-metacharacters-in-regular.html

Related Documents

4 comments:

Colin Ian King said...

shouldn't it be:

$ cat welcome.sh

and also

$ cat main.sh

Arju said...

No, the idea is from main.sh file I called welcome.sh and how dot operator helps to call. It is shown that thing in the post.
Thank you.

Colin Ian King said...

$ cat > welcome.sh

..will redirect stdout to welcome.sh, if you try this from a bash or sh shell it waits for input and redirects this into welcome.sh. Try it :-)

Arju said...

Hi Colin, I understand what you want to say. The way I did is initially no file was exist named welcome.sh. So using cat > welcome.sh I opened the file and wrote contents into it and then pressed CTRL+D to save it.

I think I should mention that. :)
Thank you.