PHP code example of reactphp-x / unbash

1. Go to this page and download the library: Download reactphp-x/unbash library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

reactphp-x / unbash example snippets




unction ReactphpX\Unbash\parse;

$ast = parse('if [ -f "$1" ]; then cat "$1"; fi');

$word = parse('echo a$(id)b')->commands[0]->command->suffix[0];

array_map(fn ($p) => $p->type, $word->parts);
// ["Literal", "CommandExpansion", "Literal"]

$src = 'echo a$(id)b';
$part = parse($src)->commands[0]->command->suffix[0]->parts[1]; // CommandExpansion
$cmd = $part->script->commands[0]->command;
substr($src, $cmd->pos, $cmd->end - $cmd->pos); // "id"

use ReactphpX\Unbash\Printer;

echo Printer::print(parse('if [ -f "$1" ]; then cat "$1"; fi'));
// if [ -f "$1" ]; then
//   cat "$1"
// fi
bash
composer install
composer test          # PHPUnit test suite
php examples/parse.php 'echo "$HOME" | tr a-z A-Z'