diff --git a/Makefile.in b/Makefile.in
index 84bcebf80..7a67928b8 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -406,22 +406,9 @@ test_interactive: $(call filter_up_to,test_interactive,$(active_test_goals))
doc_src/commands.hdr:$(HELP_SRC) doc_src/commands.hdr.in build_tools/build_commands_hdr.sh
build_tools/build_commands_hdr.sh ${HELP_SRC} < doc_src/commands.hdr.in > $@
-toc.txt: $(HDR_FILES:index.hdr=index.hdr.in) | show-SED
- @echo " SED $(em)$@$(sgr0)"
- $v rm -f toc.tmp $@
-# Ugly hack to set the toc initial title for the main page
- $v echo '- fish shell documentation - $(FISH_BUILD_VERSION)' > toc.tmp
-# The first sed command captures the page name, followed by the description
-# The second sed command captures the command name \1 and the description \2, but only up to a dash
-# This is to reduce the size of the TOC in the command listing on the main page
- $v for i in $(HDR_FILES:index.hdr=index.hdr.in); do\
- NAME=`basename $$i .hdr`; \
- NAME=`basename $$NAME .hdr.in`; \
- $(SED) <$$i >>toc.tmp -n \
- -e 's,.*\\page *\([^ ]*\) *\(.*\)$$,- \2,p' \
- -e 's,.*\\section *\([^ ]*\) *\([^-]*\)\(.*\)$$, - \2,p'; \
- done
- $v mv toc.tmp $@
+toc.txt: $(HDR_FILES:index.hdr=index.hdr.in) build_tools/build_toc_txt.sh | show-SED
+ FISH_BUILD_VERSION=${FISH_BUILD_VERSION} build_tools/build_toc_txt.sh \
+ $(HDR_FILES:index.hdr=index.hdr.in) > toc.txt
doc_src/index.hdr: toc.txt doc_src/index.hdr.in | show-AWK
@echo " AWK CAT $(em)$@$(sgr0)"
diff --git a/build_tools/build_toc_txt.sh b/build_tools/build_toc_txt.sh
new file mode 100755
index 000000000..5c6f65c26
--- /dev/null
+++ b/build_tools/build_toc_txt.sh
@@ -0,0 +1,16 @@
+#!/bin/sh
+
+# Usage: build_toc_txt.sh $(HDR_FILES:index.hdr=index.hdr.in) > toc.txt
+
+# Ugly hack to set the toc initial title for the main page
+echo "- fish shell documentation - ${FISH_BUILD_VERSION}" > toc.txt
+# The first sed command captures the page name, followed by the description
+# The second sed command captures the command name \1 and the description \2, but only up to a dash
+# This is to reduce the size of the TOC in the command listing on the main page
+for i in $@; do
+ NAME=`basename $i .hdr`
+ NAME=`basename $NAME .hdr.in`
+ env sed <$i >>toc.txt -n \
+ -e 's,.*\\page *\([^ ]*\) *\(.*\)$,- \2,p' \
+ -e 's,.*\\section *\([^ ]*\) *\([^-]*\)\(.*\)$, - \2,p'
+done