2

My bash script call a php file

#!/bin/bash
data=$(./getconfig.php)

The file getconfig.php is:

#!/usr/bin/env php
<?php
$array = array();
...

How can I return the $array from the php file in order to manipulate this array in the bash script?

2 Answers 2

4

As per documentation, an array is initialized with the following syntax:

farm_hosts=(web03 web04 web05 web06 web07)

So all you need to do is implode your $array:

echo implode(' ', $array);
Sign up to request clarification or add additional context in comments.

1 Comment

Amazing, I can't believe it's so simple. I also want to add that for some reason, bash reads the PHP array backwards (last item in PHP is first item in bash).
2

You can use the =() format to read lines into an array like this

data = (`./getconfig.php`)

You would just need to make sure your PHP script output one line for each item in the array or there is a space between each item.

The key here is using backticks to get the replacement value from PHP script output.

1 Comment

Thank you! It was necessary too!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.