Sample Header Ad - 728x90

POSIX One-Liner For Current Directory - Trying to make it pass ShellCheck

1 vote
1 answer
151 views
The one-liner from mklement0 in this discussion on POSIX-compliant scripts and getting the full path. It's failing ShellCheck. Removing the space causes it to no longer work. https://stackoverflow.com/questions/29832037/how-to-get-script-directory-in-posix-sh
dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
             ^-- SC1007: Remove space after = if trying to assign a value (for empty string, use var='' ... ).
Removing CDPATH= results in ShellCheck passing, and it still seems to work, but... > The CDPATH= prefix takes the place of > /dev/null in the original command: $CDPATH is set to a null string so as to ensure that cd never echoes anything. Looks like it's needed. So, is there any way to make this pass ShellCheck? Or just ignore it?
Asked by rannday (11 rep)
Jun 16, 2022, 12:57 PM
Last activity: Jun 16, 2022, 01:07 PM