[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Making a path absolute portably

From: Eric Blake
Subject: Re: Making a path absolute portably
Date: Wed, 10 Jul 2019 08:16:46 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0

On 7/10/19 7:54 AM, Bob Friesenhahn wrote:
> On Wed, 10 Jul 2019, Sébastien Hinderer wrote:
>> Dear all,
>> I'd need to compute the absolute path of srcdir in a portable way (it's
>> okay if it includes symlinks).
>> Currently I use
>> abssrcdir=$(cd "${srcdir}"; echo $PWD)
>> which seems to work fine.
>> Is that considered portable enough?
> It appears that $PWD is a shell extension, so I don't think so.

No, POSIX requires that $PWD always be accurate, so that part is portable.

>  Even
> the shell syntax being used to execute the external command is not very
> portable.

The use of $() instead of `` is fairly portable except to old Solaris
/bin/sh - since you asked on the autoconf list, if your usage is inside
a configure script, then we have probably already happened to re-execute
your script to be running inside a shell that supports $(), (but
surprisingly, autoconf doesn't actually yet guarantee support for $() by

>  In addition to that, if the cd "${srcdir}" fails, it would
> then capture the wrong directory.
> This syntax is proven to be portable and reliable:
> abssrcdir="`cd $srcdir && pwd`"

No, that still has problems if $srcdir contains whitespace or begins
with '-'; and also eats trailing newlines in an unusually-named
directory.  Do you have to worry about that in practice? Probably not.
But being truly portable vs. good enough can be surprisingly hard.

Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization: |

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]