4

A shebang (#!/bin/sh) is placed on the first line of a bash script, and it's usually followed on the second line by a comment describing what action the script performs. What if, for no particular reason, you decided to place the first command far beneath the shebang and the comment by, say, 10000 lines. Would that slow the execution of the script?

5

To find out, I created two shell files. Each starts with a shebang line and ends with the sole command date. long.sh has 10,000 comment lines while short.sh has none. Here are the results:

$ time short.sh 
Wed Nov 12 18:06:02 PST 2014

real    0m0.007s
user    0m0.000s
sys     0m0.004s

$ time long.sh
Wed Nov 12 18:06:05 PST 2014

real    0m0.013s
user    0m0.004s
sys     0m0.004s

The difference is non-zero but not enough for you to notice.

Let's get more extreme. I created very_long.sh with 1 million comment lines:

$ time very_long.sh
Wed Nov 12 18:14:45 PST 2014

real    0m1.019s
user    0m0.928s
sys     0m0.088s

This has a noticeable delay.

Conclusion

10,000 comment lines has a small effect. A million comment lines cause a significant delay.

How to create long.sh and very_long.sh

To create the script long.sh, I used the following awk command:

echo "date" | awk 'BEGIN{print "#!/bin/bash"} {for (i=1;i<=10000;i++) print "#",i} 1' >long.sh

To create very_long.sh, I only needed to modify the above code slightly:

echo "date" | awk 'BEGIN{print "#!/bin/bash"} {for (i=1;i<=1000000;i++) print "#",i} 1' >very_long.sh
  • Ditto +1. I'm curious, how did you get the million lines in the text editor? Some hotkey? Surely you must not have pressed the enter key down until it got to line 1 million. – user8547 Nov 13 '14 at 2:26
  • 1
    @user8547 No text editor was used. To get the million line script, just like the 10,000 line script, I used awk. The full command is shown at the end of the answer. – John1024 Nov 13 '14 at 2:30
  • 1
    A more accurate benchmark might be to have identically sized files, one with the command at line 3 and one with the command on line 10001. That way the I/O associated with reading the file is equal and the answer totally relates to the OP question. – kronenpj Nov 13 '14 at 2:30
  • 1
    @kronenpj Interesting suggestion. I just tried it with the million line file. Keeping the length of the file the same, the position of the date command in the file (line 3 vs line 1,000,002) made no difference in the timings. – John1024 Nov 13 '14 at 2:35
4

Yes, theoretically, because the shell would have to read all those blank lines and verify that they are blank.  But the effect would be so small that you'd have trouble measuring it -- especially if you tried it repeatedly (without rebooting), because, once you ran the script, it would be cached in memory, and you wouldn't even have any I/O.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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