Deutsch English Français Italiano |
<vq9nto$18ps$1@news.xmission.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!weretis.net!feeder9.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail From: gazelle@shell.xmission.com (Kenny McCormack) Newsgroups: comp.unix.shell Subject: (shellcheck) SC2103 Date: Wed, 5 Mar 2025 14:43:04 -0000 (UTC) Organization: The official candy of the new Millennium Message-ID: <vq9nto$18ps$1@news.xmission.com> Injection-Date: Wed, 5 Mar 2025 14:43:04 -0000 (UTC) Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4"; logging-data="41788"; mail-complaints-to="abuse@xmission.com" X-Newsreader: trn 4.0-test77 (Sep 1, 2010) Originator: gazelle@shell.xmission.com (Kenny McCormack) Bytes: 2874 Lines: 57 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. I find this analysis problematic for (at least) the following reasons: 1) First of all, I almost always run with either -e or (more likely) "trap handler ERR", where handler is a shell function that prints an error message and exits. So, if any "cd" fails, the script aborts. Shellcheck fails to realize this and flags every "cd" in the script with "Don't you mean: cd ... || exit". So, if it recognized the trap better, both this and the SC2103 thing would evaporate. 2) Subshells (still, as far as I know) require a fork() and run as another process. Given that bash is a large program (it says so right in the man page), this fork() is expensive. And note that it isn't the cheap sort of fork() where it is immediately followed by an exec(). It's the expensive kind which requires the COW mechanism to kick in. So, it seems unwise for shellcheck to be recommending using a subshell. I certainly avoid it if possible, for this reason. A further note: The most common case is to use (as shown above): cd somewhere;...;cd .. but the more general pattern would be: 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, but: cd - > /dev/null works. I'm guessing that people avoid using "cd -" in scripts for this reason. -- Modern Christian: Someone who can take time out from using Leviticus to defend homophobia and Exodus to plaster the Ten Commandments on every school and courthouse to claim that the Old Testament is merely "ancient laws" that "only applies to Jews".