0

If I have a bash script in my home directory and call for it to execute from my Desktop, is it possible for the script to know the request to execute came from the Desktop? I've figured out how to get the path of where the script file is, but I want to know where the request came from.

Thanks!

1 Answers1

0

Try this:

srcdir=$(pwd)

which gets you the current directory. That is, where you are calling your script from, which should be Desktop something in your example.

As duly discussed in the comments, this alternative is pretty good too:

srcdir=$PWD
Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
  • 1
    There is an environment variable `$PWD` – 123 May 17 '17 at 17:33
  • @123 you are right bro! But since OP didn't mention his OS I thought `pwd` might be a little more portable, probably doesn't matter... ;) – Paulo Mattos May 17 '17 at 17:40
  • 2
    The environment variable `PWD` and the `pwd` command are both part of the POSIX specification; they are equally portable. Typically, you would only need to use the command if you needed to use the `-L` or `-P` options for distinguishing between a symbolic link and the directory it points to. (If anything, the *variable* is more portable, since `bash` itself will set it instead of relying on the underlying operating system to provide the command.) – chepner May 17 '17 at 18:57
  • @chepner Cool, thanks for the POSIX specs info... answer updated ;) – Paulo Mattos May 17 '17 at 20:07