Re: Finding absolute path of a script from within
On Mar 14, 1:49 pm, pk <p...@pk.pk> wrote:
> Jeenu wrote:
> > Hi,
>
> > I'm wondering what would be the best way to get the absolute path of
> > script, within it. 'basename', 'dirname' and 'pwd' don't always give
> > the same output, since it depends on where you execute the script
> > from. Any thoughts?
>
> basename and dirname are simple text processing tools, which know nothing
> about the filesystem. pwd, on the other hand, usually prints correct
> information.
> However, it's not clear whether you want to know the absolute path the
> script is using as its current working dir, or the absolute path where the
> script text file is located.
>
> --
> All the commands are tested with bash and GNU tools, so they may use
> nonstandard features. I try to mention when something is nonstandard (if
> I'm aware of that), but I may miss something. Corrections are welcome.
Yeah, my intention was to get the absolute path where the script lies.
I can't use 'pwd' command inside the script, since user might be
executing it from some other directory, in which case it will output
what directory user is in. I figured out this command could do it:
where_am_i=$((cd $(dirname $0) && pwd))
Or is there any other/easier way?
|