Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What's the best way to read data from a bash configuration file (containing variable assignments) into php? Currently, my php data reads the required bash variables by assigning php variables via shell_exec. The bash code within the shell_exec statements greps for the appropriate values. An example might be: $myvar = shell_exec('grep myvar configfile.cfg | cut -d\" -f2'); However, it seems horribly inefficient to have to grep the entire configuration file for each php variable that I want to fetch. Since I'm relatively new at php, I know that there's GOT to be a better way to do this. Any suggestions?

share|improve this question
Learn how to properly open and read the file? fopen(), loop over file, reading each line, and parse out what you want. – Zoredache Oct 17 '11 at 1:35
Or keep all vars you need as associative array in $_SESSION, so you need to read it only once per session. – ott-- Oct 17 '11 at 2:57

While I would like to argue the reason why you need to do this in first place it should be a matter of reading the profile and extracting variable assignment in order to interpret these the way you want inside PHP.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.