Reputation: 6069
I extracted part of a shell script into another shell script, and for some reason after calling the sub-script, command substitution stops working. I can't imagine it's something in the sub-script, but I can't see what I'm doing wrong, either.
whether i do:
dirname "$0" # works
echo "`dirname "$0"`" # works
echo "$(dirname "$0")" # works
cat <<< "`dirname "$0"`" # works
cat <<< "$(dirname "$0")" # works
"$(dirname "$0")"/setup_buildroot.sh "$BUILDROOT" # call the sub-script
dirname "$0" # works
echo "`dirname "$0"`" # empty string
echo "$(dirname "$0")" # empty string
cat <<< "`dirname "$0"`" # crash
cat <<< "$(dirname "$0")" # crash
or i do:
dirname "$0" # works
echo "`dirname "$0"`" # works
echo "$(dirname "$0")" # works
cat <<< "`dirname "$0"`" # works
cat <<< "$(dirname "$0")" # works
"`dirname "$0"`"/setup_buildroot.sh "$BUILDROOT" # call the sub-script
dirname "$0" # works
echo "`dirname "$0"`" # empty string
echo "$(dirname "$0")" # empty string
cat <<< "`dirname "$0"`" # crash
cat <<< "$(dirname "$0")" # crash
dirname seems to work fine, but backtick and $()
stop working. The command substitution just returns an empty string.
EDIT: removing all the double quotes ""
doesn't have an effect
EDIT2: executing bash explicitly when calling the sub-script has no effect
EDIT3: saving "$(dirname "$0")"
to a variable and using that has no effect
EDIT4: adding the same echo
commands to the sub-script show that everything is fine in the sub-script
EDIT5: as per request, output of script
/home/jayen/rep
/home/jayen/rep
/home/jayen/rep
/home/jayen/rep
/home/jayen/rep
#setup_buildroot.sh output here
/home/jayen/rep
bash: syntax error near unexpected token `newline'
bash: syntax error near unexpected token `newline'
Upvotes: 2
Views: 1185
Reputation: 6069
it doesn't make sense, but try sourcing your sub-script
. "$(dirname "$0")"/setup_buildroot.sh "$BUILDROOT" # call the sub-script
Upvotes: 0
Reputation: 801
I feel like calling the sub-script is breaking the environment variables of your existing bash script's session. Maybe try prefixing the sub-script line with bash
, so
bash "`dirname "$0"`"/setup_buildroot.sh "$BUILDROOT"
and see if that fixes the issue. This is an odd situation.
The other solution I recommend is on the first line, do
mydir="$0"
and replace all the $0
with $mydir
and better yet, do
mydir=`dirname "$0"`
up front and not rerun the command over and over.
Upvotes: 1