Search

Shell Scripting With PHP: A Beginner's Guide

1 views

Shell scripting refers to scripting in command-line interpreters or shells. With it, we automate routine tasks, making processes efficient.

CLI (Command Line Interface). PHP scripts aren't just limited to browsers.

built-in functions to aid scripting.

PHP installed on your system. Use your terminal and type:

Prompt
php -v

This simple command checks the PHP version. If you see version details, you're set.

Prompt
#!/usr/bin/php <?php echo "Hello, Shell Scripting with PHP!"; ?>

Here, the #!/usr/bin/php is called a

Prompt
chmod +x hello.php

This grants execution permissions.

Prompt
./hello.php

Expect "Hello, Shell Scripting with PHP!" printed on your terminal.

Prompt
#!/usr/bin/php <?php echo "What's your name?"; $name = trim(fgets(STDIN)); echo "Hello, $name!"; ?>

command line arguments. Access them via $argv array:

Prompt
#!/usr/bin/php <?php if (isset($argv[1])) { echo "Hello, $argv[1]!"; } else { echo "Hello, Stranger!"; } ?>

Run it: ./scriptname.php YourName

Prompt
#!/usr/bin/php <?php $output = shell_exec('ls'); echo $output; ?>

This script lists directory files.

Suggest a Correction

Found an error or have a suggestion? Let us know and we'll review it.

Share this article

Comments (0)

Please sign in to leave a comment.

No comments yet. Be the first to comment!