What's a script, and what's a worktree?

A chat over two days, kept as it went: the questions as they were asked, tidied, the answers cut down, figures put in, and what turned out wrong marked where it was said.

  1. 01What a script is
  2. 02Which shell
  3. 03Which came first
  4. 04Inside an engine
  5. 05tmux
  6. 06Why split a window
  7. 07Only with another computer
  8. 08Bun again
  9. 09Two meanings of runtime
  10. 10Node's and Bun's languages
  11. 11V8 and JavaScriptCore
  12. 12Branch or worktree
  13. 13Checkout
  14. 14What a commit is
  15. 15Committing in a worktree
  16. 16A typical workflow
  17. 17One folder
01 · You

Taking all we've talked about in /usr/local/bin, parts 1 to 5 of our previous chats: I want to understand what a script is. The building blocks, and where and why it started!

Claude

A is a plain text file of commands, run by an instead of compiled into ahead of time. A compiled program like node or git is binary, built once and run directly by the processor. A script is text: when you run it, another program reads it and carries out each command. That's why cat shows you a script and shows garbage for node.

Where it started. Early Unix, at Bell Labs in the 1970s, was built from small tools that each did one thing. Its shell read the commands you typed, and someone realised it could read the same commands out of a file just as well. That file was the shell script. The motive was automation: if you type the same five commands every morning, save them and run the file.

  • The first line, #!/bin/bash, the . It looks like a comment, but it tells the system which program should read the file. #!/usr/bin/env node runs it as a Node script.
  • Commands in order, the same ones you'd type.
  • Variables: NAME="world", then $NAME.
  • Control flow: if, for and while, which turn a list into logic.
  • The execute bit: chmod +x myscript.sh, or it won't run at all.
Fig 01

The kernel starts /usr/bin/env, handing it node and this file's path. env finds node on PATH, and node reads the rest of the file.

Four files, each started from zsh. Pick one to see what the kernel makes of its first bytes.

Why it stuck. Scripts are glue: rather than write a C program to chain a few tools, you script the sequence. And they sit beside compiled programs in the same folders. Install something with npm install -g and there's a fair chance you installed a script; file on it says "text executable" instead of "Mach-O". PATH says where to look, the execute bit says whether it may run, and the first line says how.

02 · You

I can have more than two shells on my Mac? Can you tell the kernel what shell to use?

Claude

As many as you like. macOS ships zsh (the default since 2019), bash (an old version, for licensing reasons), sh, csh and tcsh, and Homebrew adds more, such as fish. A is just a program in /bin or /usr/local/bin.

The has no opinion about which you use; it runs whatever program it's pointed at. There are two separate questions:

  • Which shell greets you when a terminal opens: your login shell, one setting per account, changed with chsh -s /bin/zsh. On a Mac it's kept in Directory Services rather than /etc/passwd.
  • Which program runs a script: the script's first line, set file by file. A #!/bin/bash script runs under Bash even if your terminal opened zsh.

And inside any shell you can start another: type bash and you're in Bash, inside zsh; exit takes you back. No permission needed.

03 · You

So how did we manage to do interpretation? This is what people mean by "converted on the fly". Why do both compiled and interpreted exist, and which one came first?

Claude

A processor runs only machine code, so something has to translate everything else. The only question is when. Compiled: once, ahead of time, into a binary the processor runs with the compiler long gone. Interpreted: every time it runs, by another program that's already running and translates as it goes. That's the "on the fly".

Fig 02
  1. 1main.goSource
  2. 2go buildCompiler
  3. 3mainMachine code
  4. 4CPURuns it as it is

All of it is done before shipping. What arrives is machine code, and the processor runs it as it is: nothing else has to be installed.

Where translation happens, from your machine before shipping to the user's machine while it runs.
  • 1949 to 1951. The first assemblers, on machines such as EDSAC: a program that translates another program.
  • 1957. FORTRAN, from John Backus's team at IBM, the first successful compiler for a high-level language. Its code ran nearly as fast as hand-written assembly, which sceptics hadn't believed possible.
  • 1958. Lisp, John McCarthy's, first ran through an interpreter. McCarthy hadn't meant it to; a student built one almost by accident, and it worked well enough to stay.

They trade off in opposite directions. Compiled code runs fast but is tied to the processor it was built for, and every change means building again. Interpreted code runs as soon as you save it, anywhere the interpreter exists, and is easy to poke at live. So a toolchain uses both: node and git compiled once by their makers, your scripts and JavaScript read fresh while you iterate. And modern engines blur it: V8 interprets first, then compiles the hot parts to machine code as the program runs ().

04 · You

So when we call a JS engine, what is it exactly, under the hood?

Claude

An , V8 or SpiderMonkey or JavaScriptCore, is itself a compiled C++ program, like node or git. Its whole job is to take JavaScript text and turn it into something the processor can run, in stages: a parser, a quick interpreter running , a profiler watching what runs often, and an optimising compiler that turns hot code into machine instructions.

Fig 03
Step 1 of 5

add(a, b) is parsed into bytecode and interpreted at once. Most functions never leave this tier.

One function climbing V8's tiers, then falling back. Ink is the tier running it now.

The way back down matters too. If the compiler assumed a value was always a number and a string turns up, it throws the optimised code away and falls back to bytecode, a . That's why JavaScript's speed can be uneven: it depends on whether the engine has decided your code is worth the effort yet.

05 · You

Then the second thing: I've heard of tmux. What is it, and how did it come about?

Claude

tmux is a terminal : it splits one terminal window into sessions, windows and panes, and, crucially, keeps them running when you close the window or lose the connection.

The problem. SSH into a server, start a long job, and lose the connection: the shell running it dies, and everything it started dies with it. The lineage. GNU Screen, from 1987, was built for exactly this. tmux, by Nicholas Marriott in 2007, was a cleaner replacement: saner defaults, easier configuration, a client and server design, and a BSD licence instead of Screen's GPL.

Fig 04

The window holds zsh, and zsh holds the build.

Close the window, then open another. Each process sits under what holds it. Ink is running, dots have ended, and the window is an outline: it shows a shell, it isn't one.

A server process holds everything and survives disconnects. Sessions are workspaces; each has windows, like tabs; each window splits into panes. tmux attach from a new terminal and everything, running processes and scrollback, is as you left it.

06 · You

Wait! Why is splitting a terminal window a big deal?

Claude

Fair: splitting isn't the big deal. You could already open several windows or tabs. The value is in two things that come bundled with it.

  • Lasting through a disconnect, the real one. Close the laptop or lose wifi while connected to a server, and the import that was 80% through three hours is dead. tmux's server runs on the remote machine, apart from your connection, so the job carries on, and tmux attach brings it back.
  • One connection, many windows. When Screen and tmux were made, five windows meant five SSH logins. tmux splits one connection into as many panes as you like: a log in one, commands in another.

So splitting is the feature you see; surviving the disconnect is the problem solved. Working locally on one stable machine, it's a nice layout tool and not much more.

07 · You

So it's only useful if you have another computer? I just have a laptop with my setup.

Claude

Mostly, yes. Its headline reason only applies when there's a connection to drop: a server, a cloud machine, a Raspberry Pi. A few things still apply on a laptop alone:

  • Closing the lid. Long builds and watchers can get flaky across sleep; a tmux session survives it more reliably.
  • The habit. The day you do SSH somewhere, it's already in your fingers.
  • The terminal crashing. tmux's server is a separate process, so it outlives a crashed terminal app.

For a single laptop, tabs and Terminal's own split panes cover most of what tmux gives you. It earns its place once you work on a remote machine.

08 · You

Okay! So, going back to CLI tools, now that I understand what engines are behind the scenes. We talked about Bun. Describe it for me again; I want to connect the dots in my head.

Claude

Bun is a , the same kind of thing as Node: an engine, the interpreter and compiler pipeline we just went through, wrapped in the powers an engine doesn't have, like the file system and the network. The difference is which engine, and which language wraps it. Node is V8, Chrome's engine, wrapped in C++. Bun is JavaScriptCore, Safari's, wrapped in Zig, a newer low-level compiled language. That Zig is why bun install feels fast: compiled code doing the heavy lifting of resolving packages and writing files.

Why "all in one". A usual JavaScript project needs a stack of separate tools on your PATH: node to run, npm to install, webpack or Vite to bundle, Jest to test, tsc for TypeScript. Bun puts those jobs in one binary, bun: one compiled file with a much bigger job than most.

09 · You

Wait! I thought node, the program written in C++, is the runtime? So what is the runtime layer, then?

Claude

Good catch: the word is used two ways, and that answer slid between them. The whole program. "Node is the runtime" means the whole executable you run as node. That's what you had. A layer inside it. Node is built from two pieces: V8, the engine, and everything else, : fs, http, process, require(), the event loop.

V8 alone has no idea what a file is. It runs the language, loops, functions, objects, arithmetic, and can't reach the system. The part that lets JavaScript reach out is code the Node team wrote on top of V8. Both statements are true at once, at two levels of zoom, the way a car is a Honda and also an engine and a gearbox. Bun is the same shape: JavaScriptCore, and round it a layer for files, packages, bundling and tests.

10 · You

So both layers of Node are C++, while in Bun the engine is C++ and the runtime is Zig.

Claude

Exactly. Node is one language end to end. Bun is a split: the engine is borrowed, written by Apple in C++ for Safari, and the layer round it is Bun's own, in Zig. JavaScriptCore isn't dramatically faster than V8 at running JavaScript; the speed is concentrated in that outer layer, compiled Zig doing the file and package work.

11 · You

And the difference between V8 and JavaScriptCore is just how they work internally?

Claude

Basically, yes: the same job, engineered separately by different teams. Both parse, interpret bytecode, watch what's hot and compile it; most engines arrived at that design on their own.

  • Who built it. V8 is Google's, for Chrome, open-sourced in 2008. JavaScriptCore is Apple's, for Safari and WebKit. No shared code.
  • Tiers. V8's interpreter is Ignition and its optimising compiler TurboFan. JavaScriptCore has LLInt, then Baseline, DFG and FTL, each more aggressive than the last.
  • Memory. Both collect garbage, with different algorithms and different ways of laying objects out, which is where many real differences show.

That's why Node and Bun can behave differently at the edges: a leak, a pause, a slow path on one may not show on the other. Same contract, a different house built to keep it.

12 · You

Switching gears for a bit. I want to understand the difference between a worktree and a branch when it comes to coding. I've seen a lot of agents, or people, saying the agent is going to work in its own worktree. But we also have the idea of creating a branch, where you add a new feature or fix a bug before it's merged back into main. So what's the difference?

Claude

A branch is a pointer, not a place. It's a movable label on a commit: a tiny file holding a commit's hash. git branch feature-x copies no files and gives you no new folder. By default you have one copy of your files on disk, and switching branches rewrites that same folder in place.

A worktree is a second folder from the same repository. git worktree add ../my-feature feature-x makes a new directory with feature-x checked out, real files ready to edit, while your first folder keeps its branch, untouched.

Fig 05
  1. 1.git3 commits, every branch's
  2. 2Files in myproject/main's, checked out
  3. 3Your editNot committed
  4. 4The agentNot started

You're in myproject/ on main, halfway through an edit to header.tsx.

Step 1 of 3
You and an agent in one repository. Step through it in one shared folder, then with a worktree each.

So an agent "in its own worktree" has its own folder, on its own branch, where it can run commands, install and break things without touching yours. If it checked out a branch in your folder instead, every file would change under you. The branch is what it works on; the worktree is where.

You

Dumb it down a bit. I still haven't fully got it. Use ASCII for the visuals.

Claude

Picture your project's history as a line of snapshots. A branch is a sticky note with a name on it, stuck on one snapshot. Making a branch adds another sticky note to the same spot. No files, no folder: just a name.

The catch is that you have one folder, and it can show one branch's files at a time. Switch from main to feature-x and git doesn't make a folder; it rewrites the files in the one you have. A worktree is a real second folder, on another branch, on disk at the same time: open both in two editors and editing one doesn't touch the other.

Fig 06
myproject/shows 15fcd, “fix typo”, from main
Commit on one branch, switch, commit on the other. A dashed box is a name pointing at a commit; ink is the commit the folder shows.

Branch: which version of history you're pointing at. Worktree: a folder where that version's files sit.

13 · You

What does check out mean?

Claude

Take the files from a snapshot and write them into your folder, so what's on disk matches it. Your .git folder is a wardrobe of every outfit you've owned; your folder can wear one at a time; checking out is taking one out and putting it on.

Say commit A's index.js says "hello" and commit B's says "goodbye". Both are kept in history for good, and your folder shows one. Check out B and git rewrites the file to say "goodbye". Nothing is made or deleted in history. Checking out a branch means: find the commit that branch's note is on, and rewrite the folder to match. That's the gap a worktree fills: one folder, one checkout at a time.

14 · You

So if a branch is a label in my project files, a label at a point in time, then what is a commit?

Claude

A is the real thing: a frozen record of your whole project at one moment, with who made it, when, and a message. Once made, it never changes. A is the sticky note on one.

Every time you commit, a new snapshot is added and the branch's note slides forward onto it; the branch does nothing itself. Commit on feature-x and only its note moves, while main stays put. Two notes on two different snapshots: that's all "the branches have diverged" means.

15 · You

Is committing on a branch any different from committing in a worktree? How does that work?

Claude

No: committing doesn't know or care whether you're in a worktree. Wherever you are, git commit takes a snapshot of your files, saves it into the one shared history, and moves whichever branch is checked out in that folder onto it.

Commit in your main folder and only main moves; commit in the worktree and only its branch does. There's no such thing as committing a worktree. You always commit the branch checked out where you're standing; the worktree only decides where that is. An agent in its own worktree commits exactly as you do, onto its own branch.

16 · You

Give me an example workflow, very simplified: a developer creates a branch, works on it, moves way ahead of what's in main, and then eventually commits it to main. I just want to understand how developers typically work.

Claude
  • The set-up. You're building a website, and main is the stable version that's live.
  • Branch off. You want a dark mode, and don't want to touch main while it's half done: git checkout -b dark-mode puts a new note where main is and checks it out.
  • Work and commit, over a few days: the toggle, the theme's colours, a fix. Each commit moves only dark-mode; main stays where you left it.
  • Meanwhile, main moves too. A teammate fixes an urgent bug on it. Both lines have gone forward from the same point, and that's normal.
  • Merge back. git checkout main, then git merge dark-mode. Git makes a merge commit joining both lines, so main has the bug fix and your three commits.
  • Tidy up. git branch -d dark-mode deletes the name; its commits stay, now part of main.

Branch off, work apart, merge back: that fork-and-join shape is the whole workflow, repeated for every feature and every fix.

17 · You

Something small is confusing. If a pointer just moves forward whenever there's a new commit, then creating a branch is creating a new pointer. We edit some files, then commit, and the pointer moves forward, right? Since creating a branch isn't, technically speaking, creating a new folder, does that mean the branch we created has edited the files in the working folder we're in, which is also main's?

Claude

Yes, exactly, when there's no worktree. git checkout -b dark-mode made a new note and switched your folder to it, and since it started on the same commit as main, nothing on disk changed. Then you edited the only folder there is. It happened to be showing dark-mode, so the commit moved dark-mode's note, not main's.

main's snapshot is untouched in history; it just isn't what's on disk. Run git checkout main and git rewrites the folder back. Your dark-mode edits vanish from view, kept safe in .git, not deleted.

Without a worktree there's one folder, and the branches take turns borrowing it. A worktree gives each its own.

Terms