require 'pty' def launch_and_interact_with_terminal begin PTY.spawn("bash") do |stdout, stdin, pid| # Send a command to the terminal stdin.puts "echo 'Hello from Ruby!'" # Read the output of the command stdout.each do |line| puts line break if line.include?("Hello from Ruby!") end # You can continue to interact with the terminal here # ... # Ensure to exit the spawned shell stdin.puts "exit" end rescue PTY::ChildExited => e puts "The child process exited!" end end launch_and_interact_with_terminal