I am trying to assign the result of a sed command to a variable in bash, but I am unable to escape everything corrrectly (probably just due to my lack of knowledge in bash), I have tried:
hash_in_podfile=$( sed -rn 's/^ *pod [\'\"]XXX["\'],.*:commit *=> *["\']([^\'"]*)["\'].*$/\1/p' ${PODS_PODFILE_DIR_PATH}/Podfile )
but I am getting
bash_playground.sh: line 9: unexpected EOF while looking for matching `''
UPDATED SCRIPT
This is the script I am using updated with the code from the answer. Only the path and the comment have changed:
#!\bin\sh
PODS_PODFILE_DIR_PATH='/Users/path/to/file'
# just a comment
hash_in_podfile=$(sed -rnf - <<\! -- "${PODS_PODFILE_DIR_PATH}/Podfile"
s/^ *pod ['"]XXX["'],.*:commit *=> *["']([^'"]*)["'].*$/\1/p
!
)
echo $hash_in_podfile
executed with sh script_name.sh
sh --version
yields:
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin20) Copyright (C) 2007 Free Software Foundation, Inc.
On execution I get:
script_name.sh: line 6: unexpected EOF while looking for matching `"'
script_name.sh: line 10: syntax error: unexpected end of file
There are two issues in your script:
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 installing a newer bash
shell from the Homebrew package manager (or equivalent), or by using the zsh
shell on macOS.
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 the option-argument to -f
to mean "read from standard input". Use /dev/stdin
instead.
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
If all you want to do is to output the value, then don't use an intermediate variable:
#!/bin/zsh
PODS_PODFILE_DIR_PATH='/Users/path/to/file'
# just a comment
sed -n -f /dev/stdin -- $PODS_PODFILE_DIR_PATH/Podfile <<'END'
s/^ *pod ['"]XXX["'],.*:commit *=> *["']([^'"]*)["'].*$/\1/p
END