[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Why does a shell script need current directory?
From: |
Chet Ramey |
Subject: |
Re: Why does a shell script need current directory? |
Date: |
Mon, 15 Feb 2021 09:34:53 -0500 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 |
On 2/13/21 9:17 PM, Peng Yu wrote:
The part of the paragraph that you omitted makes it pretty clear
that a value of PWD inherited in the environment must correspond
to the current working directory (which necessarily has to exist)
to be considered. Even then, there are many cases in which the
shell has license to ignore it.
I have to say the POSIX manual is confusing. According to the
following text, if PWD is specified from the environment, does shell
have to check the absolute pathname of the current working directory?
Yes. It doesn't have to check if the value from the environment is not
an absolute pathname.
"""
If a value for PWD is passed to the shell in the environment when it
is executed, the value is an absolute pathname of the current
working directory that is no longer than {PATH_MAX} bytes including
the terminating null byte, and the value does not contain any
components that are dot or dot-dot, then the shell shall set PWD to
the value from the environment.
"""
If so, this doesn't make too much sense. If the shell needs to check
the absolute path of the current working directory anyway, why does it
bother whether PWD is specified in the environment. It seems that this
double requirement is either unnecessary, or I misunderstood
something.
It's easier to stat "." and the value passed in the environment and check
whether they refer to the same directory. Most of the time they do. Then,
since you have to have the right value of PWD to pass in the environment,
you call getcwd only if that check fails.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/