Shell integration

Justin Hileman edited this page Jun 3, 2017 · 1 revision

💲 System shell integration

It can be super useful to access a system shell from inside a REPL session. PsySH fully supports this … but mostly because PHP supports this 😛.

Execute arbitrary shell commands

To execute arbitrary shell commands, wrap the command in backticks.

>>> `pwd`
=> "/Projects/psysh\n"
>>> `ls`
=> """
   CONTRIBUTING.md\n
   LICENSE\n
   README.md\n
   bin\n
   composer.json\n
   composer.lock\n
   phpunit.xml.dist\n
   src\n
   test\n
   vendor\n
   """
>>>

PHP variables can be interpolated into shell commands as well.

>>> $readme = 'README.md'
=> "README.md"
>>> `ls -al $readme`
=> "-rw-r--r--  1 justin  staff  9649 May 28 13:34 README.md\n"
>>>

✨ Shell commands with magic variables

Some commands—such as doc, dump, ls and show—update the $__file, $__dir and $__line magic variables after they run. These variables are especially useful when interpolated into shell commands.

For example, after using doc to show the documentation for a class or method, you can use the subl shell command to open that file in Sublime Text and jump to the definition.

>>> doc Psy\Shell
class Psy\Shell extends Symfony\Component\Console\Application

Description:
  The Psy Shell application.

  Usage:

      $shell = new Shell;
      $shell->run();

Author: Justin Hileman <justin@justinhileman.info>
>>> $__file
=> "/Projects/psysh/src/Psy/Shell.php"
>>> `subl $__file:$__line`
=> null
>>>

This. Is. So. Good.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.