Skip to content

Instantly share code, notes, and snippets.

@mckern
Last active February 18, 2020 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mckern/6aa5317a548d1b09f5cb574e43858def to your computer and use it in GitHub Desktop.
Save mckern/6aa5317a548d1b09f5cb574e43858def to your computer and use it in GitHub Desktop.
Your home-brew bathtub `realpath` hacks suck

A simple example:

ryan@C02X43Q9JG5K tmp.kU0KEN9U $ tree .
.
├── fakepaths
│   └── bin
│       └── shell.sh -> ../realpaths/bin/shell.sh
└── realpaths
    ├── bin
    │   └── shell.sh
    └── lib
        └── shell.lib.sh

5 directories, 3 files

A simple script:

ryan@C02X43Q9JG5K tmp.kU0KEN9U $ cat realpaths/bin/shell.sh
#!/usr/bin/env bash

set -x

realpath="$(cd $(dirname "$0") && pwd)"

[[ -f ${realpath}/../lib/shell.lib.sh ]] &&
  echo "found shell lib" ||
  echo "no shell lib found!"

And oh no, surprise, it doesn't actually work.

ryan@C02X43Q9JG5K tmp.kU0KEN9U $ ./realpaths/bin/shell.sh
+++ dirname ./realpaths/bin/shell.sh
++ cd ./realpaths/bin
++ pwd
+ realpath=/var/folders/pg/p4vzhf114sx6n2zmf_lz_df00000gn/T/tmp.kU0KEN9U/realpaths/bin
+ [[ -f /var/folders/pg/p4vzhf114sx6n2zmf_lz_df00000gn/T/tmp.kU0KEN9U/realpaths/bin/../lib/shell.lib.sh ]]
+ echo 'found shell lib'
found shell lib
ryan@C02X43Q9JG5K tmp.kU0KEN9U $ ./fakepaths/bin/shell.sh
+++ dirname ./fakepaths/bin/shell.sh
++ cd ./fakepaths/bin
++ pwd
+ realpath=/var/folders/pg/p4vzhf114sx6n2zmf_lz_df00000gn/T/tmp.kU0KEN9U/fakepaths/bin
+ [[ -f /var/folders/pg/p4vzhf114sx6n2zmf_lz_df00000gn/T/tmp.kU0KEN9U/fakepaths/bin/../lib/shell.lib.sh ]]
+ echo 'no shell lib found!'
no shell lib found!
ryan@C02X43Q9JG5K tmp.kU0KEN9U $
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment