Editing
Integration of CodeRunner with MAMP
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
= CodeRunner PHP Run Command: Auto-Detecting the Version = # [https://coderunnerapp.com/ CodeRunner – Programming Editor for macOS] # [https://www.mamp.info/en/mac/ MAMP] In Settings → Languages → PHP → Run Command, the default configuration usually looks like this: <pre lang="bash"> /path/to/php $filename </pre> For example: <pre lang="bash"> /Applications/MAMP/bin/php/php8.2.29/bin/php $filename </pre> 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: <pre lang="bash"> /bin/sh -c 'php=$(ls -d /Applications/MAMP/bin/php/php*/bin/php | sort -V | tail -1); "$php" "'"$filename"'"' </pre> == Overall Structure == <pre> /bin/sh -c ' command string ' </pre> <code>-c</code> tells <code>/bin/sh</code> to treat the following text as a shell command to execute. Everything inside the single quotes <code>'...'</code> is the actual script being run, just packed onto one line. == Step 1: Finding the newest php executable == <pre lang="bash"> php=$(ls -d /Applications/MAMP/bin/php/php*/bin/php | sort -V | tail -1) </pre> <table class="wikitable"> <tr> <td> Segment </td> <td> What it does </td> </tr> <tr> <td> <code>ls -d /Applications/MAMP/bin/php/php*/bin/php</code> </td> <td> Lists all files matching this path pattern </td> </tr> <tr> <td> <code>sort -V</code> </td> <td> Sorts by '''version number''' </td> </tr> <tr> <td> <code>tail -1</code> </td> <td> Takes the last line of the sorted output — the newest version </td> </tr> <tr> <td> <code>php=$(...)</code> </td> <td> Stores the result into the variable <code>php</code> </td> </tr> </table> '''What <code>ls -d</code> does''': Normally, <code>ls</code> on a directory lists its contents, but <code>-d</code> makes it list the matching path itself without descending into it. <code>php*</code> is a wildcard matching every folder under <code>php</code> that starts with <code>php</code>, for example: <pre> /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 </pre> '''What <code>sort -V</code> does''': Regular <code>sort</code> sorts as plain strings, which causes problems — <code>php8.10.0</code> would sort ''before'' <code>php8.2.0</code> (since character <code>1</code> is less than <code>2</code>). <code>-V</code> (version sort) understands version-number semantics and correctly places <code>8.10.0</code> after <code>8.2.0</code>. '''<code>tail -1</code>''': Takes the last line of the sorted result — the entry with the highest version number. So the whole <code>$(...)</code> expression captures the full path to the newest installed MAMP php version. After this line runs, the <code>php</code> variable holds something like: <pre> /Applications/MAMP/bin/php/php8.2.29/bin/php </pre> == Step 2: Actually running php == <pre lang="bash"> "$php" "$filename" </pre> * <code>"$php"</code>: retrieves the path stored earlier, wrapped in double quotes so it won't break into multiple arguments if the path contains spaces. * <code>"$filename"</code>: a built-in CodeRunner variable, automatically replaced with the path of the currently open file. This is equivalent to: <pre lang="bash"> /Applications/MAMP/bin/php/php8.2.29/bin/php "/path/to/php_code_runner.php" </pre> == Why there's a <code>;</code> in between == <pre lang="bash"> php=$(...); "$php" "$filename" </pre> <code>;</code> is the command separator, meaning "finish this command, then do the next one" — same as typing two separate lines: # First, find the php path and store it. # Then, use that path to run the file. == Why <code>$filename</code> uses that odd quoting pattern == CodeRunner handles <code>$filename</code> by directly replacing the text <code>$filename</code> 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 <code>'...'</code> for <code>sh -c</code>, the shell won't expand anything inside those single quotes at all. If <code>$filename</code> were left inside the single quotes, it would be treated as the '''literal text''' <code>$filename</code>, not the actual path CodeRunner substitutes in. The fix is to "step out" of the single-quoted string right where <code>$filename</code> needs to go, let CodeRunner substitute it outside the quotes, then step back into the single-quoted string: <pre> "'"$filename"'" </pre> Broken down: <pre> "' → 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) </pre> 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 == # CodeRunner first replaces <code>$filename</code> with the actual file path as plain text (a pure string substitution that happens before the command reaches the shell). # The resulting full string is passed to <code>/bin/sh -c</code> for execution. # The shell runs <code>ls | sort -V | tail -1</code> to dynamically scan for the newest installed MAMP php executable path, storing it in the <code>php</code> variable. # That path is used to run your php file, with the file path passed as an argument. '''The advantage''': it doesn't depend on <code>which</code>, aliases, or <code>$PATH</code> at all — because CodeRunner's execution environment doesn't load shell config files like <code>.zshrc</code> (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. [[Category: Programming]] [[Category: PHP]] [[Category: Revised with LLMs]]
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information