I have a large bash script and I need to rerun certain lines of code. Normally I would comment out any line I don't want to run and then execute the file on the shell. But that is rather cumbersome. I'd much rather select the lines I want to execute and do something like M-x execute-selected-lines-in-the-current-directory. Is there something like that?

  • What does the substring in-the-current-directory in the assumed command name execute-selected-lines-in-the-current-directory mean? Could you elaborate please? Do you want to run the selected lines in a directory different from the default-directory of the current shell script file buffer?
    – Tobias
    Commented Feb 25, 2019 at 15:23

2 Answers 2


Use M-| (shell-command-on-region) then sh to pipe the current region to Bash. It works because bash supports running program from STDIN, e.g.,

~ $ echo 'date' | sh
Mon Feb 25 21:54:51 CST 2019

M-| runs via a fresh shell every time, so I'm not sure if it can work in your case.


You could try sh-execute-region which is bound to C-M-x in sh-mode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.