The short question is: I need to launch an interactive bash in a certain directory, and I can craft the command that launches bash, but I can't modify the system. At the moment, the command that best fits my needs is
bash -c 'cd some-directory; bash'
That is, I am calling bash, to use cd
within bash, and then creating a child instance of bash within that to handle my interactive bashing.
I'm aware this isn't the most egregious bash usage in the world, but it feels weird to me. Is there an easier way to do what I'm looking for?
Just for unnecessary context:
This is a part of my tooling that deals with launching a shell within a docker instance (using docker exec) via docker-compose. I could find myself inside any of about 15 docker containers that represent projects - some of them have a "default" directory that I would want to be in, and others wouldn't. The tooling is meant to bridge the gap, and give me a smoother experience switching from one project to another.
Some things that I have tried that I thought might work but didn't include:
PWD='~/some/directory' bash
- this just gets ignoredbash -i -c "cd ~/some/directory
- it creates a shell marked as interactive, but doesn't wait for any input like a normal interactive shell.echo "cd ~/some/directory" | bash --rcfile -"
was worth a try
Some things that I know would work but don't in my case:
- Change the working directory then launch my command: I'm in docker, so my host's working directory doesn't matter. I'm intrigued as to how bash knows what the current working directory of a parent shell is.
docker-compose
does have a-w
that will take an absolute path, doesn't know how to handle tildes.cd ~/some/directory ; bash
- docker-compose requires an actual executable- Put it in an rcfile in the container, or create a "launcher" script: Kinda works, but I want to be able to configure it from the outside, and don't want to edit each of the containers
So is there a way to set the initial directory that I've overlooked, or is this just the way it's done? I know that shell integrations such as Gnome's "Open folder in terminal" must be doing something to tell bash what folder to start in, I can't work out what magic it is using though.
env
(likely if you havebash
) it has an option to change the working dir for a program it execs:env -C /wanted/dir bash
– dave_thompson_085 Jan 26 '24 at 04:18