Reputation: 11
I have exported my script to PATH
variable. So from anywhere, I may invoke my script but I have to know from which path I am invoking the script.
Script present in the path /home/raja/scps/shell/s1.sh
export PATH=$PATH:/home/raja/scps/shell/s1.sh.
I will run the script as follows:
1st example:
root@raja-H97-D3H:/home/raja#s1.sh
I want to know the path: /home/raja
2nd example:
root@pmt-H97-D3H:/home/pmt/tmp/p1/p2#s1.sh
I want to know the path: /home/pmt/tmp/p1/p2
Upvotes: 1
Views: 219
Reputation: 21213
The current working directory from where the script was invoked is available in the PWD
environment variable inside the script.
Consider this example script:
#!/bin/bash
echo $PWD
Add it to your path, and invoke it from any directory. $PWD
will have what you want.
This:
root@raja-H97-D3H:/home/raja# s1.sh
Would output /home/raja
.
This:
root@pmt-H97-D3H:/home/pmt/tmp/p1/p2# s1.sh
Would output /home/pmt/tmp/p1/p2
.
Also, you want to add the directory with the script to $PATH
; not the script file itself. You should do this instead:
export PATH=$PATH:/home/raja/scps/shell
Upvotes: 3