Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 28 |
Nodes: | 6 (1 / 5) |
Uptime: | 45:01:37 |
Calls: | 422 |
Calls today: | 1 |
Files: | 1,024 |
Messages: | 90,303 |
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
)
The ostensible rationale is that it is shorter/easier to code, but the real rationale is that if the cd fails, putting it into a subshell "localizes"
the damage.
cd somewhere;...;cd -
But note that "cd -" - even in a script - prints the name of the directory
it is cd'ing back to, which is annoying. I could not find any option to
turn this off,
In this use case, the command
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.
[...]
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.
[...]
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
)
The ostensible rationale is that it is shorter/easier to code, but the real rationale is that if the cd fails, putting it into a subshell "localizes"
the damage.
but the more general pattern would be:
cd somewhere;...;cd -
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.
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.
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.
However, most of the actions in a shell script fork and exec
something anyway.
Does
cd -- somedir &&
{
do_something
cd ..
}
make shellcheck happy while at the same time localizing the
damage and avoiding a subshell?
cd somewhere;...;cd -
But note that "cd -" - even in a script - prints the name of the directory >> it is cd'ing back to, which is annoying. I could not find any option to
turn this off,
cd -- "$OLDPWD"
Will help.
In article <20250305103210.358@kylheku.com>,
Kaz Kylheku <643-408-1753@kylheku.com> 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.
That's actually mentioned in the rationale on the web page.
That if you need to set a variable, you can't use subshells.
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.
However, most of the actions in a shell script fork and exec
something anyway.
Yes, but those would be the cheap form of fork(), where if it is quickly followed by an exec(), you don't have to do COW.
Note that, in Linux, fork() is actually an alias for vfork().