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.

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.

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 :-)

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

This question has already been answered. Start a new discussion instead.