Dot operator in Scripting.
Using the Unix Dot (.) Operator to Run a Shell Script in the Current Shell
In unix, there is the dot operator. The syntax is a dot (period), followed by a space, and followed by a shell script:
. script
Why would you use the dot operator?
Normally, when you run a shell script, it executes in a child shell. Once the script completes, the child shell goes away and you are returned to the command prompt on the original shell. This means that the scope of the script doesn't apply to the invoking shell.
In other words, if your script changed the directory or set some variables, those changes won't be reflected after the script ends.
Using a dot space in front of the script means that the scope of the script is the same as the invoking shell.
For example, let's say that you are currently in the /usr directory and you run the script go_tmp, which changes the directory to /tmp.
If you run go_tmp then, after the script ends, you will still be in /usr.
If you run . go_tmp, then you will be in /tmp after it runs.
As another example, suppose you have a script set_java_classpath, which modifies your CLASSPATH environment variable so that you can run java programs. You will need the dot operator (i.e. run . set_java_classpath) so the new path is still there after the script ends.
The dot operator is also used a lot in shell scripts that need to call other scripts (like subroutines).
. script
Why would you use the dot operator?
Normally, when you run a shell script, it executes in a child shell. Once the script completes, the child shell goes away and you are returned to the command prompt on the original shell. This means that the scope of the script doesn't apply to the invoking shell.
In other words, if your script changed the directory or set some variables, those changes won't be reflected after the script ends.
Using a dot space in front of the script means that the scope of the script is the same as the invoking shell.
For example, let's say that you are currently in the /usr directory and you run the script go_tmp, which changes the directory to /tmp.
If you run go_tmp then, after the script ends, you will still be in /usr.
If you run . go_tmp, then you will be in /tmp after it runs.
As another example, suppose you have a script set_java_classpath, which modifies your CLASSPATH environment variable so that you can run java programs. You will need the dot operator (i.e. run . set_java_classpath) so the new path is still there after the script ends.
The dot operator is also used a lot in shell scripts that need to call other scripts (like subroutines).
Comments
Post a Comment