Sujet : Re: (shellcheck) SC2103
De : nn.throttle (at) *nospam* xoxy.net (Helmut Waitzmann)
Groupes : comp.unix.shellDate : 05. Mar 2025, 23:09:52
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <83ikonf7tb.fsf@helmutwaitzmann.news.arcor.de>
References : 1 2
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Kaz Kylheku <
643-408-1753@kylheku.com>:
On 2025-03-05, Kenny McCormack <gazelle@shell.xmission.com> wrote:
All testing done with shellcheck version 0.10.0 and bash under Linux.
>
Shellcheck says that you should replace code like:
>
cd somedir
do_something
cd .. # (Or, cd -, which is almost, but not exactly the same thing)
>
with
>
(
cd somedir
do_something
)
>
That obviously won't work if do_something has to set a variable
that is then visible to the rest of the script.
>
Forking a process just to preserve a current working directory
is wasteful; we wouldn't do that in a C program, where we might
open the current directory to be saved, and then fchdir back to it.
>
Unfortunately, there is no way to fchdir() by means of a shell built‐in command, say “fchdir“.
cd somewhere;...;cd -
>
cd - will break if any of the steps in between happen to to cd;
it is hostile toward maintenance of the script.
>
Yes. I really would appreciate using the imagined “fchdir” bash built‐in command to be able to do set_exit_status()
{
return ${1-}
}
unset -v es &&
exec {saved_wd}< . &&
{
cd -- somedir &&
{
do_something
es="$?"
fchdir -- "$saved_wd"
}
exec {saved_wd}<&-
set_exit_status ${es-}
}