Exit the shell script

Hi,

suppose my script is sample.sh
i have to run using '. ./sample.sh'
as . ./script file always executes the script in my parent shell.
when my sample.sh contains exit command .. my environment is getting closed as am executing in the parent shell ...
please suggest me how can i use exit command when am executing a shell script using '. ./' format.

. ./script is equivalent to typing the commands in script one by one on the terminal.

This method of invoking (or dotting) a file is used when we need to export a lot of parameters for use by current shell.

If you want to use exit and don't want to exit the terminal then "execute" the script by using ./script after checking the execute permission on the script.

If you have any particular reason to invoke the script like parameter export scenario mentioned above, separate those statements alone into a separate script and invoke it using the dot space format.

Hi Krishmaths,

Thanks for your quick response..
can you elaborate more on ./script
my script name is logproc, as i dont want to exit my terminal should I execute as
./logproc
I tried this but following error occured
-bash: ./logproc.sh : No such file or directory.
Please let me know how can i execute my script without exiting the terminal(. ./filename execution is mandatory)

The return command can be used instead of exit if the script is sourced (invoked using . ./script.sh).

1 Like

If your script name is logproc either navigate to the directory where the script is available and execute as ./logproc or execute using absolute path /my/dir1/logproc (Replace /my/dir1) with the directory name where the script is available.

If the script name has a .sh extension remember to include that as well while executing.

And, be sure that the file has been made executable:

chmod +x ./script.sh