Skip to content

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 <[email protected]>
>>> $__file
=> "/Projects/psysh/src/Psy/Shell.php"
>>> `subl $__file:$__line`
=> null
>>>

This. Is. So. Good.