Self-editing scripts 📜

Thrig suggests that I was wrong about shell scripts being read in their entirety at start up

...and talks about tracing and IO in a convincing way, and it sounds like I'd got it wrong. So I experimented.

testedit.sh

#!/bin/bash
echo start
sed -i testedit.sh -e 's/middling/surprise/'
echo middling
echo end

Output

start
middling
end

The script was modified on disk while running, but it wasn't re-read. Only if I run it a second time do I get a surprise.

Have I misunderstood something?

Batch version

@echo off
echo start
sed -i testedit.bat -e s/middling/surprise/
echo middling
echo end

Output

start
surprise
end

This time there's a surprise. After sed exited, the batch file was re-read.

#ShellScript
back to gemlog