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
    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.

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .