views:

14440

answers:

8

Basically I need to run the script with paths related to the shell script file location, how can I change the current directory to the same directory as where the script file resides?

+24  A: 

In bash you should get what you need like this:

#!/bin/bash

BASEDIR=$(dirname $0)
echo $BASEDIR
TheMarko
But he has hinted the most important part. I end up doing:cd `dirname $0`Works like a charm.Perhaps Marko might want to revise this a bit?
goodwill
My comment saying that this was incorrect was incorrect. When I used the "load new answers" link, what it returned was incorrectly formatted, and had no backticks
Mez
This will not work if the script is in your path.
sharth
This doesn't work if you've called the script via a symbolic link in a different directory. To make that work you need to use `readlink` as well (see al's answer below)
AndrewR
+6  A: 

Assuming you're using bash

#!/bin/bash

current_dir=$(pwd)
script_dir=$(dirname $0)

echo $current_dir
echo $script_dir

This script, when ran, should print the directory that you're in, and then the directory the script is in, for example, when calling it from / (the script is in /home/mez/), it outputs

/
/home/mez

Remember, when assigning variables from the output of a command, wrap the command in $( and ) - or you'll not get the desired output. `

Mez
Yours is clearer, but Marko indeed point out the main point :) so I still give you 1 upvote
goodwill
I agree with goodwill. +1
TheMarko
Yeah, no, a bug made the code block show up incorrect (and bad) code
Mez
No harm done and the $() is still a valid point :)
TheMarko
TheMarko, twas actually because of the non-indentation that caused me to see BASEDIR=dirname $0 echo $BASEDIRWhich would basically run it over and over again. Greg fixed that, making me wrong :'(
Mez
A: 

I'm not sure how to do it, but neither of the previous answers work. This is because $0 is the command as called. If you call the script foo like this './foo', then $0 = ./foo, not /path/to/foo like you want.

If you call the script like './foo', then pwd will be the current directory.
R. Bemrose
A: 

echo \pwd\/\dirname $0\

that should do the trick -- it might look ugly depending on how it was invoked and the cwd but should get you where you need to go (or you can tweak the string if you care how it looks)

A: 

echo \pwd\/\dirname $0\ would NOT work in case that the script would be called with absolute path

+5  A: 

Have a look at http://fritzthomas.com/open-source/linux/384-how-to-get-the-absolute-path-within-the-running-bash-script

The original post contains the solution (ignore the responses, they don't add anything useful). The interesting work is done by the mentioned unix command "readlink" with option -f. Works when the script is called by an absolute as well as by a relative path.

Here a slightly modified copy of the solution in case the other post vanishes....

This is for bash, sh, ksh:

#!/bin/bash 
# Absolute path to this script, e.g. /home/user/bin/foo.sh
SCRIPT=`readlink -f $0`
# Absolute path this script is in, thus /home/user/bin
SCRIPTPATH=`dirname $SCRIPT`
echo $SCRIPTPATH

Almost the same for tcsh, csh:

#!/bin/tcsh
# Absolute path to this script, e.g. /home/user/bin/foo.csh
set SCRIPT=`readlink -f $0`
# Absolute path this script is in, thus /home/user/bin
set SCRIPTPATH=`dirname $SCRIPT`
echo $SCRIPTPATH
al
A: 

If you're using bash....

#!/bin/bash

pushd $(dirname "${0}") > /dev/null
basedir=$(pwd -L)
# Use "pwd -P" for the path without links. man bash for more info.
popd > /dev/null

echo "${basedir}"
The Doctor What
A: 

This checks if directory exists and is writable

if [ -d "$Directory" -a -w "$Directory" ]
then
    #Statements
fi

-Muralikrishna.B

muralikrishna