954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Dot operator and shell scripting

I have a script called setuptex that is run on a terminal as

. ~/context/tex/setuptex


I wanted to execute it from a bash script file instead of typing it each time on an interactive terminal.

Since the dot operator and the source builtin command are similar, and I wanted a simple one word command, I tried scripting it thus:

#! /bin/bash
source ~/context/tex/setuptex


saved it in a file, and having made the file executable , executed it from a console. It does not seem to have any effect, whereas running the interactive version with the dot operator works as expected.

Can someone please explain why and also how to make the script file work as intended.

polygon
Newbie Poster
8 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 

It is important to realize that the child process cannot possibly affect the parent's environment. When you run the command from the executable file, a second copy of bash is forked, it has its environment changed and then dies leaving no trace. The builtins (dot and source for example) are executed in the context of the current process, without forking a child, and the environment modifications stay put.
To make a simple one word command, you may wrap it in the function, and have that function in .bashrc, or some other autosourced file of your preference.

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 
It is important to realize that the child process cannot possibly affect the parent's environment. When you run the command from the executable file, a second copy of bash is forked, it has its environment changed and then dies leaving no trace. The builtins (dot and source for example) are executed in the context of the current process, without forking a child, and the environment modifications stay put. To make a simple one word command, you may wrap it in the function, and have that function in .bashrc, or some other autosourced file of your preference.

Thanks, nezachem for your explanation.

I now have in my ~/.bashrc file the following alias:

alias setuptex=". ~/context/tex/setuptex"


and then executing

setuptex


within a shell does exactly what I want it to :-)

polygon
Newbie Poster
8 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 

First you need to have the permissions checked for your file...they should have the execute permission to the owner(assuming u are the owner).

Also executing the shell script with a "." operator executes the script in the same shell. whereas executing it with a command ksh, bash, etc. will execute it in a subshell.

When you are executing it from a script, you are including the bash commadn to execute all your scripts that are called from the current script so it will execute, which btw does not check for file permissions.
When you run it with the dot operator you should have the necessary permissions to execute the script. use chmod to change permissions of the script.

Regards
Vaibhav

vaibhav1983
Junior Poster
147 posts since Aug 2009
Reputation Points: 12
Solved Threads: 12
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: