Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

16
  • 54
    I'm not quite sure why you expected Bash to be faster than Python. Commented Aug 13, 2016 at 8:49
  • 10
    @MatijaNalis no you can't! The script is loaded into memory, editing the text file it was read from (the script file) will have absolutely no effect on the running script. A good thing too, bash is already slow enough without having to open and re-read a file every time a loop is run! Commented Aug 13, 2016 at 12:05
  • 7
    Related: Why is using a shell loop to process text considered bad practice? Commented Aug 13, 2016 at 15:44
  • 6
    Bash reads the file line-by-line as it executes, but it remembers what it read if it comes to that line again (because it's in a loop, or a function). The original claim about re-reading each iteration isn't true, but modifications to yet-to-be-reached lines will be effective. An interesting demonstration: make a file containing echo echo hello >> $0, and run it. Commented Aug 14, 2016 at 10:28
  • 3
    @MatijaNalis ah, OK, I can understand that. It was the idea of changing a running loop that threw me. Presumably, each line is read sequentially and only after the last one has finished. However, a loop is treated as a single command and will be read in its entirety, so changing it won't affect the running process. Interesting distinction though, I had always assumed that the entire script is loaded into memory before execution. Thanks for pointing it out! Commented Aug 14, 2016 at 13:57