0

I'm working on an interactive Ruby script, which build and packages resources. In the middle of the process, I'd like to drop into an interactive shell, but pre-cd'd into a specific working directory, as well as with an explanatory message (CTRL-D to continue). The interactive bash + given initial command is what's problematic.

Per the answer for doing something like this in Bash, given at https://stackoverflow.com/a/36152028, I've tried

system '/bin/bash', '--init-file', '<(echo "cd ~/src/devops; pwd")'

However, bash runs interactively but completely ignores the '<(echo "cd ~/src/devops; pwd")' section.

Interestingly system '/bin/bash', '--init-file complains if no argument is given, but literally anything runs bash, but with no initial command.

*Note that (--rcfile instead of --init-file) has the same effect.

Community
  • 1
  • 1
Excalibur
  • 3,258
  • 2
  • 24
  • 32
  • You can just change the current working directory of the Ruby script before launching `bash`; the working directory is inherited. – chepner May 19 '17 at 18:07

1 Answers1

1

Change the working directory of the Ruby script first, so that bash inherits the correct working directory.

curr_dir = Dir.pwd
Dir.chdir("#{Dir.home}/src/devops")
system "/bin/bash"
Dir.chdir(curr_dir)   # Restore the original working directory if desired

Oh, this is probably far better (you can probably guess how little familiarity I have with Ruby):

system("/bin/bash", :chdir=>"#{Dir.home}/src/devops")
chepner
  • 497,756
  • 71
  • 530
  • 681