How can I get the path that the file is in? For example, my script is running in ~/Desktop/Projects/Shell but it doesn't know that. How can I get it to know that? Like DIR = DIR_IM_IN?

Recommended Answers

All 9 Replies

pwd will print the working directory. Or, ${PWD}

pwd gives the current directory, the one your in when you're calling the script, not the one where is the script.

see man bash /^ *0

man bash /^ *0

Excuse me? What does see man bash /^ *0 mean?

will print the working directory. Or,

That prints the DIR that it's being called in, not the directory of the script itself.

Shamelessly stolen from the interwebs.

pushd `dirname $0` > /dev/null
SCRIPTPATH=`pwd -P`
popd > /dev/null

What does see man bash /^ *0 mean?

open bash's man page, and type /^ *0
/ is to search
^ *0 is line which begins with space(s) and a zero.

Shamelessly stolen from the interwebs.
1. pushd dirname $0 > /dev/null
2. SCRIPTPATH=pwd -P
3. popd > /dev/null

nice trick, why not just cd

cd $(dirname $0)
scriptPath=$(pwd -P)
cd ~-

I also saw this one

scriptPath=$(readlink -f $(dirname $0))

Shamelessly stolen from the interwebs.

I found that one on the internet, too. But, I couldn't get it to run in Python. You see, my script is not actually Shell. But I'm executing a Shell Script INSIDE of python. I suppose I should use shell to execute everything... the problem is I know little about shell :P

But for the purposes I'm using it, I'm sure I can handle

Thanks!

What does see man bash /^ *0 mean?

open bash's man page, and type /^ *0
/ is to search
^ *0 is line which begins with space(s) and a zero.

Oh...

-bash: /^: No such file or directory

That's a problem.
I'm running a Mac so it might be different than linux, if you have it. Thanks anyway!

The other scripts work though, thanks!

You see, my script is not actually Shell.

It would be nice in the future if you could explain your problem clearly (along with any solutions that you have tried) so that we are not all wasting our time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.