1

In my use case, I want to spawn a shell with current directory set to a specific one for users to perform some operations, the problem is user often have their PS1 set to print current working directory and I don't want a long ridiculous path to show up if the directory they've specified is a soft linked one (to that same directory but with a way shorter path name, see my example below).

However this behavior seems to be everywhere and I don't know if there is a way around:

$ cd /tmp
$ mkdir -p some/ridiculously/long/path/name/that/no/one/wants/to/see
$ ln -s /tmp/some/ridiculously/long/path/name/that/no/one/wants/to/see s
$ ghci
GHCi, version 8.8.3: https://www.haskell.org/ghc/  :? for help
Prelude> import System.Directory
Prelude System.Directory> setCurrentDirectory "/tmp/s"
Prelude System.Directory> getCurrentDirectory 
"/tmp/some/ridiculously/long/path/name/that/no/one/wants/to/see"

Same goes for turtle's cd / pushd, which seems to use system-fileio instead of directory, but the same behavior remains. I know CreateProcess of process has a cwd record field I can set when I'm about to spawn the shell but that seems to do the same. So I've run out of ideas on what else to try.

Javran
  • 3,394
  • 2
  • 23
  • 40
  • 1
    I don't think this is possible. It's not really possible to “be in” a directory that actually is just a symlink at all. When you do this in Bash, what's actually happening is that the `$PWD` variable gets updated independently of the actual directory-changing mechanism, that's why the prompt then shows the short form – but notice that if you e.g. open a new terminal tab, this will also start out with the expanded form of the path. – leftaroundabout Feb 16 '21 at 21:59
  • An alternative you could consider is to _loop-mount_ the directory, instead of just symlinking it. That's maybe a bit overkill, but in this case it really would be possible to be in the short-form directory while still having it perfectly synced with the long-form one. Or, write your own wrappers around `setCurrectDirectory` / `getCurrentDirectory` that store the “PWD as specified” in addition to the “real PWD”. – leftaroundabout Feb 16 '21 at 22:02
  • @leftaroundabout Good point! I've noticed that before but wasn't fully aware that PWD updates independent of the actual chdir detail, but it now makes sense to me operationally. I was considering an alternative, if it's not possible within Haskell calls, that could this be done by passing command line arguments to shell, or spawn the shell, then literally typing in "cd " and giving back control to user afterwards - but probably that's too hacky and I'll settle with status quo. – Javran Feb 16 '21 at 23:03
  • 1
    oh I've found a trick to do just that: `$SHELL -c "cd ; $SHELL"`, thanks to https://serverfault.com/a/834344, not very pleasant but gets the job done. – Javran Feb 16 '21 at 23:12

0 Answers0