0

Running this in bash in my Homestead box works: symbRefHead=$(git symbolic-ref -q HEAD) && echo $symbRefHead && branch_name="${symbRefHead//refs\/heads\/}" && echo $branch_name

So why does running it via runLocally fail?

$options = ['tty' => true];
    runLocally('symbRefHead=$(git symbolic-ref -q HEAD) && echo $symbRefHead && branch_name="${symbRefHead//refs\/heads\/}" && echo $branch_name', $options);

I get this error:

sh: 1: Bad substitution

In Process.php line 250:

  The command "symbRefHead=$(git symbolic-ref -q HEAD) && echo $symbRefHead && branch_name="${symbRefHead//refs\/heads\/}" && echo $branch_name" failed.

  Exit Code: 2(Misuse of shell builtins)

Similarly this version works in Bash but not in runLocally: symbRefHead=$(git symbolic-ref -q HEAD) && echo $symbRefHead && to_be_removed="refs/heads/" && branch_name="${symbRefHead/$to_be_removed/}" && echo $branch_name

I'm obviously trying to remove a substring from a string variable (search and replace with nothing) as mentioned here: https://unix.stackexchange.com/questions/104881/remove-particular-characters-from-a-variable-using-bash#comment437753_104887

P.S. Ultimately I'll want to be able to get the branch name into a variable so I can echo it here: https://stackoverflow.com/a/59689871/470749

Ryan
  • 22,332
  • 31
  • 176
  • 357
  • 1
    The parameter substitution you are using is not a POSIX shell feature, but runLocally is executing sh rather than bash. – jordanm Feb 06 '20 at 17:14
  • 1
    Why not just run `git symbolic-ref -q HEAD` with a subprocess and then do the output parsing withing php? – jordanm Feb 06 '20 at 17:15
  • @jordanm Thank you for your hint about sh rather than bash! I don't understand your second comment. Thanks in advance. (You could post an answer.) – Ryan Feb 06 '20 at 17:22

0 Answers0