Deutsch   English   Français   Italiano  
<vqa73f$2huko$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Janis Papanagnou <janis_papanagnou+ng@hotmail.com>
Newsgroups: comp.unix.shell
Subject: Re: (shellcheck) SC2103
Date: Wed, 5 Mar 2025 20:02:05 +0100
Organization: A noiseless patient Spider
Lines: 51
Message-ID: <vqa73f$2huko$1@dont-email.me>
References: <vq9nto$18ps$1@news.xmission.com> <20250305103210.358@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 05 Mar 2025 20:02:07 +0100 (CET)
Injection-Info: dont-email.me; posting-host="c7b28c7b2c5da4ab81c18f27a162cf8b";
	logging-data="2685592"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/qhEmJ97d2SXIggF95Jnn7"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
 Thunderbird/45.8.0
Cancel-Lock: sha1:RrPRGEwrLJkAKSCNJnRNEMwQY0g=
In-Reply-To: <20250305103210.358@kylheku.com>
X-Enigmail-Draft-Status: N1110
Bytes: 2534

On 05.03.2025 19:40, Kaz Kylheku wrote:
> 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.

Indeed. Only for strict hierarchical semantics it makes sense.

> 
> 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.

Shells may be different. While Bash regularly creates a subprocess,
Ksh in certain structures creates just a "subshell context" without
forking/cloning an own process.

> [...]
> 
>> but the more general pattern would be:
>>
>>     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.

Indeed.

Just note that spending a "subshell context"[Ksh] or a subprocess
[Bash] keeps the structure intact. (If you want to pay for that;
especially when it's costly [as in Bash].)

Janis

> [...]