While trying to determine the differences between .bashrc and .bash_profile I notice that .bashrc is never read, and that .bash_profile is only read from SSH, but not from PHP. This seems to contradiction to the information in man bash. Could someone explain to me why this works as it does? The server seems to be a CentOS 5 variant.local$ ssh user@serverLast login: Wed Jan 23 23:21:23 2013 from 1.2.3.4$ cat .bashrcalias br='echo fromBR'$ cat .bash_profilealias bp='echo fromBP'$ br-bash: br: command not found$ bpfromBP$ cat public_html/bashtest.php <?phpecho hello\n;echo shell_exec('bp');echo shell_exec('br');?>$ php public_html/bashtest.phphellosh: bp: command not foundsh: br: command not found$ wget server/bashtest.php23:35:13 (759.55 KB/s) - `bashtest.php' saved [7/7]$ cat bashtest.php hello!So it seems that .bashrc is never read and .bash_profile is only read from the CLI (not from PHP either on the user's CLI nor through Apache). Furthermore, through Apache the command not found text isn't even returned!Is this expected behaviour (i.e. am I misinterpreting man bash) or is something amiss?
Bash config files being read in contradiction to `man bash`
linux;bash;centos;php
Well, for a start, PHP isn't doing shell_exec through bash in your case, it's doing it through sh. This is fairly obvious from the exact error message. I'm guessing that this is controlled by whatever shell is specified in /etc/passwd for the user that the web server is running as and shell_exec doesn't capture stderr, in combination with that when you run PHP from the command line it simply drops out to ${SHELL}. When launched as sh, bash turns off a number of features to better mimic the behavior of the original sh shell. Sourcing of .bashrc and .bash_profile almost certainly are among those, if for no other reason then because those files are likely to use bash-specific syntax or extensions.I'm not really sure about the SSH case, but judging from the plain $ prompt, you might very well be running through sh there, which would likewise explain the behavior you are seeing. Try echo ${SHELL} to see what you really got dropped into; that should work on all shells.That said, it seems to me like a really bad idea to depend on bash aliases from a PHP script. If what you want to do is too long to fit nicely in the shell_exec statement itself (which should only be used with great care), making a PHP function to create the command line from the parameters and calling that is almost certainly a much better approach, and it will work essentially regardless of which shell is installed, selected or how it is configured. Alternatively, consider calling an external script file, which can be written in bash and specify /bin/bash as its interpreter. But then your application will require that bash is installed (which it probably does already if it depends on bash aliases...).