I have this code in a bash script:
r2g(){
echo "executable is: $0" # "/bin/bash"
(
set -e;
r2g_internal "$@" \
2> >( while read line; do echo "r2g error: $line"; done ) \
1> >( while read line; do echo "r2g: $line"; done )
)
exit_code="$?"
if [[ "$exit_code" != "0" ]]; then
echo "something experienced an error, to see log, run: r2g_view_log";
return 1;
fi
}
what's happening is that after r2g_internal
runs, sh is launched by some process and it apparently tries to resource the bash env, and I get his weird syntax error from sh:
r2g error: sh: r2g: line 2: syntax error near unexpected token `>'
r2g error: sh: r2g: line 2: ` r2g_internal "$@" 2> >( while read line; do echo "r2g error: $line"; done ) > >( while read line; do echo "r2g: $line"; done ) );'
r2g error: sh: error importing function definition for `r2g'
I made a video demoing the problem: https://www.useloom.com/share/82f23ebfe6754412a20be057957e45f4
and a follow up vid: https://www.useloom.com/share/0465c2857cc244879b52b7bdb516243e
when npm install
runs, some sh
process must be launched by npm
..git
also seems to launch an sh
process sometimes when I run git commands, and the same type of syntax errors show up in the terminal in that case.
I cannot figure out why a sh process launched via bash would then try to source some of the bash code from the parent shell/env?
The video makes the problem clearer (I hope).
if I call unset -f r2g
, the problem goes away immediately. So I guess /bin/sh
is calling my r2g function, but I don't know how or why.
Here is the source for r2g and r2g_internal together: https://gist.github.com/ORESoftware/0fa7e3d6b75a65b17b6b126a7bec3397
sh
is reading the script file that you intend for bash. – Jeff Schaller May 09 '18 at 00:45sh
is definitely evaluating the script, but whysh
is evaluating the script is not clear – Alexander Mills May 09 '18 at 00:48r2g_internal
code – cuonglm May 09 '18 at 01:26/bin/bash
, I show that in the video – Alexander Mills May 09 '18 at 05:46>(...)
and[[
. These are bashisms (Well, technically, process substitution originated in ksh ), but the point is that/bin/sh
doesn't have these, and if you use/bin/sh
, make sure your script uses only the stuff that it understands. If there is no portability requirement or POSIX compiance, then just use/bin/bash
and call it a day. – Sergiy Kolodyazhnyy May 10 '18 at 01:45sh
someone else is...the mystery is not thatsh
is being called by some subprocess, the mystery is whysh
is invokingr2g
– Alexander Mills May 10 '18 at 01:57postinstall.sh
doing in your video and does that have anything to do with it? – Kusalananda May 10 '18 at 06:45