Fork me on GitHub
#babashka
<
2022-09-09
>
minikomi01:09:49

hey there just joined. enjoying all borkdude's interesting directions & work!

🎉 6
Søren Sjørup11:09:53

Is there a way to change working directory(`cd`) for the calling process? Essentially making babashka behave as cd?

borkdude11:09:33

No this isn't possible, it's a JVM limitation. You can however spawn a new bb process within a new directory, if that helps.

Søren Sjørup11:09:12

Thank you I thought so… It wouldn’t really help I made a script that clone git repos into the file structure I like. I wanted it to also be able to jump to the new dir.

borkdude11:09:39

That's not even possible in bash, you can't change the cwd of the parent process

borkdude11:09:08

you can however create a new shell within a new directory after you clone

Søren Sjørup11:09:38

No makes sense. Thank you for the, as always, quick response!

Crispin13:09:26

yes, cd is a bash built-in. There is no binary called cd on your computer. Try which cd and it will come back blank.

Crispin13:09:56

you can use source perhaps to achieve what you want

Crispin13:09:03

crispin@vash:~$ echo "cd /etc" > test.sh
crispin@vash:~$ chmod a+x test.sh
crispin@vash:~$ ./test.sh 
crispin@vash:~$ source test.sh 
crispin@vash:/etc$ 

Crispin13:09:37

running ./test.sh runs it in a subshell

Crispin13:09:09

but running source test.sh evaluates the bash commands in the present shell context

Crispin13:09:29

your programme would have to spit out bash for you to source though... might be tricky

Søren Sjørup13:09:47

Thanks crispin! So I guess I would write a bash wrapper that calls my clone script that returns the path to cd to.

borkdude13:09:06

I think spawning a new shell from your script is easier though.

borkdude13:09:47

bb -e '(babashka.process/shell {:dir "/tmp"} (System/getenv "SHELL"))'

Crispin13:09:55

some virtual language wrappers (like virtualenv in python or rbenv in ruby) spit out a script that you source to "activate" the virtual env

Søren Sjørup13:09:24

@U04V15CAJ I get an Exception when I try to ^D out of the shell that command starts. Is that expected?

borkdude13:09:16

it's probably not an exception but just the printed process

borkdude13:09:36

you can also use exec to replace bb with the shell:

bb -e '(babashka.process/exec [(System/getenv "SHELL")] {:dir "/tmp"})'

Søren Sjørup13:09:18

Nice, that one shuts down nicely.

Crispin13:09:15

another way:

crispin@vash:~$ `bb -e '(println "cd /etc")'`
crispin@vash:/etc$ 

borkdude13:09:41

ah, exec doesn't allow changing directories

Crispin13:09:57

I think $() is the modern backticks

borkdude13:09:59

@U021UJJ3CQ6 This should work:

bb -e '(do (babashka.process/shell {:dir "/tmp"} (System/getenv "SHELL")) nil)'

👍 2
borkdude13:09:09

this won't print the process

Crispin13:09:13

crispin@vash:~$ $(bb -e '(println "cd /etc")')
crispin@vash:/etc$ 

Søren Sjørup13:09:33

@U04V15CAJ Thank you very much the both of you. It now works as I wanted! See the full script here if you want: https://gist.github.com/zoren/371baf6afbc2327832bb86ae70d5c4cd

👍 1