Pages

Monday, November 24, 2014

source command in UNIX

Source command can be used to run the shell code in the current terminal. If we are running a bash script otherwise it will be running in a separate new terminal and after the end of the shell script it will come back to the current shell. So if we are writing the alias and all in the script it will not work on the current shell after the execution of the script since it is in separate context.

Syntax:
=======
source current.sh

current.sh - use back to go back once from the current directory
=========
#!/bin/sh
alias back="cd .."

# you can add the required shorts over this file and run by using the source to make it applicable to the current shell.

The other use of the source is to add separate bash script file which have some functions, alias or some other initialization and we need to use these in a new script. Here just we can use source command. For example :

new.sh - to go back twice from the current directory
=======
#!/bin/sh
source current.sh
back
back

Now call source new.sh to go back twice from the current directory..

No comments:

Post a Comment