shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

request: warn SC2103 on popd

Open bugsbugsbux opened this issue 11 months ago • 3 comments

SC2103 is: "Use a ( subshell ) to avoid having to cd back."

cd home
cd -       # warns SC2103
pushd home
popd       # doesn't warn SC2103, but should imo

bugsbugsbux avatar Feb 02 '25 19:02 bugsbugsbux

@bugsbugsbux can you elaborate why you want this? From my understanding, the cd in a shell script can cause side effects outside the script. So you always need to cd back to avoid this. As far as I know, this is not the case with pushd/pops. Hence there is no warning needed.

grische avatar Aug 05 '25 10:08 grische

When I filed this issue I thought SC2103 was a hint to avoid a bad pattern, which I would have liked to know about before writing lots of pushd /; do-sth; popd and only came across when I used cd /; do-sth; cd - instead in some other script.

the cd in a shell script can cause side effects outside the script. So you always need to cd back to avoid this.

I don't think so: If you put cd / or pushd / in a script and execute it with bash myscript.sh or ./myscript.sh you end up where you started, on the other hand if you source any of the two scripts you end up in /.

Both, popd and cd - can fail or produce unexpected outcomes: cd - always fails before the first directory change (but not afterwards because it just switches back and forth between the current and previous folders) "-bash: cd: OLDPWD not set", popd always fails if there is no previous entry in the directory stack "-bash: popd: directory stack empty". Both commands should be conditional on the success of the corresponding previous directory change; or better avoided alltogether as SC2103 suggests.

bugsbugsbux avatar Aug 14 '25 18:08 bugsbugsbux

IMO, it's best to rely as little as possible on the filesystem. It can be changed by some other process during script execution, is one reason. If it's necessary, a data directory with a non-predictable name created in an atomic way in a dependable base directory is a solid way to go.

On Thu, Aug 14, 2025, 11:45 bugsbugsbux @.***> wrote:

bugsbugsbux left a comment (koalaman/shellcheck#3135) https://github.com/koalaman/shellcheck/issues/3135#issuecomment-3189515540

When I filed this issue I thought SC2103 was a hint to avoid a bad pattern, which I would have liked to know about before writing lots of pushd /; do-sth; popd and only came across when I used cd /; do-sth; cd - instead in some other script.

the cd in a shell script can cause side effects outside the script. So you always need to cd back to avoid this.

I don't think so: If you put cd / or pushd / in a script and execute it with bash myscript.sh or ./myscript.sh you end up where you started, on the other hand if you source any of the two scripts you end up in /.

Both, popd and cd - can fail or produce unexpected outcomes: cd - always fails before the first directory change (but not afterwards because it just switches back and forth between the current and previous folders) "-bash: cd: OLDPWD not set", popd always fails if there is no previous entry in the directory stack "-bash: popd: directory stack empty". Both commands should be conditional on the success of the corresponding previous directory change; or better avoided alltogether as SC2103 suggests.

— Reply to this email directly, view it on GitHub https://github.com/koalaman/shellcheck/issues/3135#issuecomment-3189515540, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUF2F2ZXK3OHNOSHCCDXUJL3NTKMBAVCNFSM6AAAAABWKWXXROVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTCOBZGUYTKNJUGA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

wileyhy avatar Aug 17 '25 19:08 wileyhy