Integration of CodeRunner with MAMP

From LemonWiki共筆
Revision as of 10:21, 15 July 2026 by Planetoid (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

CodeRunner PHP Run Command: Auto-Detecting the Version

  1. CodeRunner – Programming Editor for macOS
  2. MAMP


In Settings → Languages → PHP → Run Command, the default configuration usually looks like this:

/path/to/php $filename

For example:

/Applications/MAMP/bin/php/php8.2.29/bin/php $filename

The problem is that every time MAMP upgrades its PHP version, you have to come back and manually update this path. Changing it to the line below lets CodeRunner automatically detect the "currently installed newest version" every time it runs:

/bin/sh -c 'php=$(ls -d /Applications/MAMP/bin/php/php*/bin/php | sort -V | tail -1); "$php" "'"$filename"'"'

Overall Structure

/bin/sh -c '  command string  '

-c tells /bin/sh to treat the following text as a shell command to execute. Everything inside the single quotes '...' is the actual script being run, just packed onto one line.

Step 1: Finding the newest php executable

php=$(ls -d /Applications/MAMP/bin/php/php*/bin/php | sort -V | tail -1)
Segment What it does
ls -d /Applications/MAMP/bin/php/php*/bin/php Lists all files matching this path pattern
sort -V Sorts by version number
tail -1 Takes the last line of the sorted output — the newest version
php=$(...) Stores the result into the variable php


What ls -d does: Normally, ls on a directory lists its contents, but -d makes it list the matching path itself without descending into it. php* is a wildcard matching every folder under php that starts with php, for example:

/Applications/MAMP/bin/php/php8.1.33/bin/php
/Applications/MAMP/bin/php/php8.2.29/bin/php
/Applications/MAMP/bin/php/php8.3.14/bin/php

What sort -V does: Regular sort sorts as plain strings, which causes problems — php8.10.0 would sort before php8.2.0 (since character 1 is less than 2). -V (version sort) understands version-number semantics and correctly places 8.10.0 after 8.2.0.

tail -1: Takes the last line of the sorted result — the entry with the highest version number. So the whole $(...) expression captures the full path to the newest installed MAMP php version.

After this line runs, the php variable holds something like:

/Applications/MAMP/bin/php/php8.2.29/bin/php

Step 2: Actually running php

"$php" "$filename"
  • "$php": retrieves the path stored earlier, wrapped in double quotes so it won't break into multiple arguments if the path contains spaces.
  • "$filename": a built-in CodeRunner variable, automatically replaced with the path of the currently open file.

This is equivalent to:

/Applications/MAMP/bin/php/php8.2.29/bin/php "/path/to/php_code_runner.php"

Why there's a ; in between

php=$(...); "$php" "$filename"

; is the command separator, meaning "finish this command, then do the next one" — same as typing two separate lines:

  1. First, find the php path and store it.
  2. Then, use that path to run the file.

Why $filename uses that odd quoting pattern

CodeRunner handles $filename by directly replacing the text $filename in the Run Command field with the actual file path string — it's not spawning a shell first and passing in a variable. In other words, CodeRunner does text substitution, not shell variable expansion.

Since the whole command is wrapped in single quotes '...' for sh -c, the shell won't expand anything inside those single quotes at all. If $filename were left inside the single quotes, it would be treated as the literal text $filename, not the actual path CodeRunner substitutes in.

The fix is to "step out" of the single-quoted string right where $filename needs to go, let CodeRunner substitute it outside the quotes, then step back into the single-quoted string:

"'"$filename"'"

Broken down:

"'   →  closing double quote + closing single quote (temporarily exits the string)
"$filename"  →  outside the single quotes, so CodeRunner replaces it with the actual file path
'"   →  opening single quote + opening double quote (re-enters the string)

This is a common shell trick: inserting content that needs to be expanded in the middle of a single-quoted string. Since single quotes never expand anything inside them, the only way to splice in something that needs expanding is to end the single-quoted string, insert the piece, then start a new single-quoted string right after.

Summary of the Flow

  1. CodeRunner first replaces $filename with the actual file path as plain text (a pure string substitution that happens before the command reaches the shell).
  2. The resulting full string is passed to /bin/sh -c for execution.
  3. The shell runs ls | sort -V | tail -1 to dynamically scan for the newest installed MAMP php executable path, storing it in the php variable.
  4. That path is used to run your php file, with the file path passed as an argument.

The advantage: it doesn't depend on which, aliases, or $PATH at all — because CodeRunner's execution environment doesn't load shell config files like .zshrc (where aliases are defined), scanning the filesystem directly for the path is the most reliable approach. And future MAMP php version upgrades won't require manually updating this setting again.