Scripting / Scripts

A script is a light-weight program usually written in an interpreted language such as Bash, Python, JavaScript, Node, Perl, or Ruby.

[Not to be confused with scriptures.]

The script file itself is not what executes. The interpreter is a binary that executes on the computer and reads in the text of your script, compiles it, and then runs it on your behalf.

For example, put the following into a helloloop text file and run it with bash.

while true; do
  echo hello world
  sleep 1

⚠️ Do not forget the sleep 1.

Run it with bash helloloop and you will see something like the following:

bash helloloop
hello world
hello world
hello world
hello world

Let it run and open another terminal.

Let’s see what it looks like in the process table.

ps -eF | grep hello
rob      9745  8473  0  3289  3476   0 19:31 pts/7    00:00:00 bash helloloop

Notice that bash is the thing running not helloloop.

💡 Try to find bash helloloop using the tree utility to see it’s parents.

You can Ctrl-c your helloloop now to cancel it.

Scripts are Horribly Inefficient

Because every script must be compiled every single time it is run (even though bytecode does help sometimes) it takes a very long time — relatively — to even start your script. A lot of people don’t care about this, but you should. This is why you should never execute a script from another script or program unless you absolutely must. This is a common source of slowing down systems in general.

💎 If you have a script that you use a lot consider rewriting it in a compiled language like Go or Rust or C.

💬 This is actually the main reason that Python is not the great language so many people think it is and you should only use it if you must.

To give you a sense here are the benchmarks on the two: