# frozen_string_literal: true require 'pty' def launch_and_interact_with_terminal 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 puts 'The child process exited!' end launch_and_interact_with_terminal