0

I am trying to traverse the fp in babashka, and have found that running (shell "cd ..") in my script bb-test causes an error:

----- Error --------------------------------------------------------------------
Type:     java.io.IOException
Message:  Cannot run program "cd": error=2, No such file or directory
Location: /home/jack/Documents/clojure/leingit/./bb-test:120:1

Any ideas?

Jack Gee
  • 136
  • 6
  • `cd` is a shell internal. It can not be called like a regular program. You can run it via `sh -c 'cd ..'`, but that is just a no-op. It would help if you could shed light on why you want to change the cwd or what you plan to do in the next steps. – cfrick Oct 22 '22 at 10:05
  • @cfrick, I want to do some git commands in a newly made directory – Jack Gee Oct 22 '22 at 12:53

2 Answers2

3

You can't change the working directory in babashka (it is a limitation that comes from being a JVM-derived environment). But you can spawn new processes in other directories:

(require '[babashka.process :refer [shell]])

(shell {:dir ".."} "whatever")
Michiel Borkent
  • 34,228
  • 15
  • 86
  • 149
1

cd is shell built-in; you can not call it outside a shell. So what you want to do instead is run your command with a changed "current working directory". This can be done in sh with the :dir option. E.g.

(-> (shell/sh "ls" :dir "/etc") :out)

This will list the content of /etc from wherever you are calling your script.

(as mentioned in the comments of the question, this is about calling git commands: git also allows for changing its root via the GIT_WORK_TREE env-var, which could be changed via the :env option from sh, but changing the cwd is more straightforward)

cfrick
  • 35,203
  • 6
  • 56
  • 68