1

Is there a way to check within a shell script (ksh) whether or not the script was started in the current shell?

Example

Start script in current shell with . (dot/source) command

$ . ./myscript
$ I run in the current environment!


Start script in own process

$ ./myscript
$ I run in my own process!
TechnoCore
  • 1,394
  • 2
  • 16
  • 21

2 Answers2

0

This is a simple trick you can use.

#!/bin/ksh
if [ ${.sh.file} != ${0} ]; then
    echo I run in the current environment
else
    echo I run in my own process
fi
alvits
  • 6,550
  • 1
  • 28
  • 28
  • `${.sh.file}` leads to error message `bad substitution` on my side. – TechnoCore Apr 14 '14 at 06:07
  • @TechnoCore - I realized you might not be using GNU `ksh`. You cam simply replace `.sh.file` with `_`. The statement will then be `if [ ${_} != ${0} ]; then`. – alvits Apr 14 '14 at 20:29
0

Every Shell Has its Own PID ..

so you can use echo "$$" in ur script ..it will helps us find from where the Script is RAN .

i.e Difference in pid means they are run from different shells .

pravin_23
  • 128
  • 2
  • 2
  • 11