Skip to main content
1 of 3
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k

There are two issues in your script:

  1. The sh on macOS is a very old version of the bash shell, and it has a bug that stops you from using unbalanced quotes in here-documents in command substitutions:

    $ a=$( cat <<'END'
    > "
    > END
    > )
    > sh: unexpected EOF while looking for matching `"'
    

    (I had to press Ctrl+D after the ) at the end there.)

    You can get by this by using bash from Homebrew or by using the zsh shell on macOS.

  2. The sed on macOS does not have an -r option. To use extended regular expressions with sed on macOS, use -E (this is also supported by GNU sed nowadays). Your expression does not use extended regular expression features though, so just removing the option would be ok too. macOS sed also can't use - as standard input. Instead use /dev/stdin.

Suggestion:

#!/bin/zsh

PODS_PODFILE_DIR_PATH='/Users/path/to/file'

# just a comment

hash_in_podfile=$(sed -n -f /dev/stdin -- "${PODS_PODFILE_DIR_PATH}/Podfile" <<'END'
s/^ *pod ['"]XXX["'],.*:commit *=> *["']([^'"]*)["'].*$/\1/p
END
)

echo "$hash_in_podfile"
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k