Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs workflow to edit Bash scripts while they run

Tags:

bash

emacs

Bash can give unexpected results if one edits a script while the script is running. But it would often be very convenient to be able to edit a temporary copy of a script that I run within a shell, but make sure it is back in the original location before I run it again. Does anyone have suggestions for a workflow or customizations to Emacs to facilitate this?

like image 770
Michael Hoffman Avatar asked Dec 01 '11 02:12

Michael Hoffman


People also ask

Can you modify a Bash script while it is running?

The shell reads and executes commands one by one, so the simple answer is "yes, editing a running script can affect it", but it's important to understand the details. If you are careful, you can add lines to the end of a running script, and they will be run after the other commands already in the script.

Why do bash scripts start with #!?

Bash has evolved over the years taking code from ksh and sh . Adding #!/bin/bash as the first line of your script, tells the OS to invoke the specified shell to execute the commands that follow in the script. #! is often referred to as a "hash-bang", "she-bang" or "sha-bang".

Does bash use multithreading?

Parallel executes Bash scripts in parallel via a concept called multi-threading. This utility allows you to run different jobs per CPU instead of only one, cutting down on time to run a script.


1 Answers

M-x delete-file, press down to insert obtain the path to the file you're editing, and RET to delete the file. When you next save your buffer, this will create a new file. Bash will keep executing the old, deleted file.

By the way, in most cases, you don't even need to take this precaution. Emacs usually saves to a temporary file, then renames that temporary file to the proper file name (which deletes the old file, unless it's being kept as a backup). Emacs only overwrites the file in place if it detects that it can't recreate the file properly: if the file has hard links, or if you don't have write permission on the directory. (I'm simplifying a little; the details are in the basic-save-buffer-2 function in files.el.) As long as the file has a single directory entry and you have write permissions on the directory containing the script, just press C-x C-s.

like image 94
Gilles 'SO- stop being evil' Avatar answered Nov 10 '22 19:11

Gilles 'SO- stop being evil'