Shell code cleanup

- Argument mixes string and array. Use * or separate argument.
- Quote the grep pattern so the shell won't interpret it.
- Use "${var:?}" to ensure this never expands to /* .
- > is for string comparisons. Use -gt instead.
- Quote the parameter to -name so the shell won't interpret it.

Closes #4163.

PiperOrigin-RevId: 179042046
diff --git a/scripts/bootstrap/buildenv.sh b/scripts/bootstrap/buildenv.sh
index 5c5061b..8dfb6fe 100755
--- a/scripts/bootstrap/buildenv.sh
+++ b/scripts/bootstrap/buildenv.sh
@@ -38,7 +38,7 @@
     exitCode=1
   fi
   echo >&2
-  echo "ERROR: $@" >&2
+  echo "ERROR: $*" >&2
   exit $exitCode
 }
 
diff --git a/scripts/bootstrap/compile.sh b/scripts/bootstrap/compile.sh
index e817b19..c7b26cc 100755
--- a/scripts/bootstrap/compile.sh
+++ b/scripts/bootstrap/compile.sh
@@ -17,11 +17,11 @@
 # Script for building bazel from scratch without bazel
 
 PROTO_FILES=$(ls src/main/protobuf/*.proto src/main/java/com/google/devtools/build/lib/buildeventstream/proto/*.proto)
-LIBRARY_JARS=$(find third_party -name '*.jar' | grep -Fv /javac-9-dev-r3297-4.jar | grep -Fv /javac-9-dev-4023-3.jar | grep -Fv /javac7.jar | grep -Fv JavaBuilder | grep -Fv third_party/guava | grep -Fv third_party/guava | grep -ve third_party/grpc/grpc.*jar | tr "\n" " ")
+LIBRARY_JARS=$(find third_party -name '*.jar' | grep -Fv /javac-9-dev-r3297-4.jar | grep -Fv /javac-9-dev-4023-3.jar | grep -Fv /javac7.jar | grep -Fv JavaBuilder | grep -Fv third_party/guava | grep -Fv third_party/guava | grep -ve 'third_party/grpc/grpc.*jar' | tr "\n" " ")
 GRPC_JAVA_VERSION=1.7.0
-GRPC_LIBRARY_JARS=$(find third_party/grpc -name '*.jar' | grep -e .*${GRPC_JAVA_VERSION}.*jar | tr "\n" " ")
+GRPC_LIBRARY_JARS=$(find third_party/grpc -name '*.jar' | grep -e ".*${GRPC_JAVA_VERSION}.*jar" | tr "\n" " ")
 GUAVA_VERSION=23.1
-GUAVA_JARS=$(find third_party/guava -name '*.jar' | grep -e .*${GUAVA_VERSION}.*jar | tr "\n" " ")
+GUAVA_JARS=$(find third_party/guava -name '*.jar' | grep -e ".*${GUAVA_VERSION}.*jar" | tr "\n" " ")
 LIBRARY_JARS="${LIBRARY_JARS} ${GRPC_LIBRARY_JARS} ${GUAVA_JARS}"
 
 # tl;dr - error_prone_core contains a copy of an older version of guava, so we
diff --git a/scripts/serve-docs.sh b/scripts/serve-docs.sh
index f1dfbd5..c4541a8 100755
--- a/scripts/serve-docs.sh
+++ b/scripts/serve-docs.sh
@@ -41,7 +41,7 @@
 
 build_tree() {
   bazel build //site:jekyll-tree.tar
-  rm -rf $WORKING_DIR/*
+  rm -rf ${WORKING_DIR:-sentinel}/*
   tar -xf "$(bazel info bazel-genfiles)/site/jekyll-tree.tar" -C $WORKING_DIR
 }
 
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt
index 682126d..9689927 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt
@@ -99,7 +99,7 @@
   if [[ "$ARG" == --wrapper_script_flag=* ]]; then
     process_wrapper_argument "${ARG#--wrapper_script_flag=}" \
       || die "invalid wrapper argument '%s'" "$ARG"
-  elif [[ "${#ARGS}" > 0 ]] || ! process_wrapper_argument "$ARG"; then
+  elif [[ "${#ARGS}" -gt 0 ]] || ! process_wrapper_argument "$ARG"; then
     ARGS+=( "$ARG" )
   fi
 done
diff --git a/src/main/native/windows/build_windows_jni.sh b/src/main/native/windows/build_windows_jni.sh
index 16e149f..a15da4a 100644
--- a/src/main/native/windows/build_windows_jni.sh
+++ b/src/main/native/windows/build_windows_jni.sh
@@ -23,7 +23,7 @@
 shift 1
 
 function fail() {
-  echo >&2 "ERROR: $@"
+  echo >&2 "ERROR: $*"
   exit 1
 }
 
diff --git a/src/test/shell/bazel/bazel_rules_test.sh b/src/test/shell/bazel/bazel_rules_test.sh
index db897b1..1396eca 100755
--- a/src/test/shell/bazel/bazel_rules_test.sh
+++ b/src/test/shell/bazel/bazel_rules_test.sh
@@ -18,7 +18,7 @@
 #
 
 function die() {
-  echo >&2 "ERROR[$(basename "$0") $(date +%H:%M:%S.%N)] $@"
+  echo >&2 "ERROR[$(basename "$0") $(date +%H:%M:%S.%N)] $*"
   exit 1
 }
 
diff --git a/src/test/shell/integration/java_integration_test.sh b/src/test/shell/integration/java_integration_test.sh
index e780b8b..f4bf19b 100755
--- a/src/test/shell/integration/java_integration_test.sh
+++ b/src/test/shell/integration/java_integration_test.sh
@@ -317,7 +317,7 @@
   # https://github.com/bazelbuild/bazel/issues/3156
   local -r first_run="$(md5_file $(find "${PRODUCT_NAME}-out/" -type f '!' \
       -name build-changelist.txt -a '!' -name volatile-status.txt \
-      -a '!' -name stderr-* -a '!' -name *.a \
+      -a '!' -name 'stderr-*' -a '!' -name '*.a' \
       -a '!' -name __xcodelocatorcache -a '!' -name __xcruncache \
       | sort -u))"
 
@@ -328,7 +328,7 @@
       || fail "Build failed"
   local -r second_run="$(md5_file $(find "${PRODUCT_NAME}-out/" -type f '!' \
       -name build-changelist.txt -a '!' -name volatile-status.txt \
-      -a '!' -name stderr-* -a '!' -name *.a \
+      -a '!' -name 'stderr-*' -a '!' -name '*.a' \
       -a '!' -name __xcodelocatorcache -a '!' -name __xcruncache \
       | sort -u))"
 
diff --git a/src/test/shell/integration/loading_phase_tests.sh b/src/test/shell/integration/loading_phase_tests.sh
index a183010..f2ec898 100755
--- a/src/test/shell/integration/loading_phase_tests.sh
+++ b/src/test/shell/integration/loading_phase_tests.sh
@@ -137,7 +137,7 @@
        expect_not_log .  # print the log
     }
   done
-  [ ${#topics[@]} -gt 15 ] || fail "Hmmm: not many topics: ${topics[@]}."
+  [ ${#topics[@]} -gt 15 ] || fail "Hmmm: not many topics: ${topics[*]}."
 }
 
 # Regression for "Sticky error during analysis phase when input is cyclic".
diff --git a/src/test/shell/integration/progress_reporting_test.sh b/src/test/shell/integration/progress_reporting_test.sh
index f011994..7d9ea60 100755
--- a/src/test/shell/integration/progress_reporting_test.sh
+++ b/src/test/shell/integration/progress_reporting_test.sh
@@ -54,7 +54,7 @@
   kill -9 "$pid"
   # A fail() here would not end the script if we are in a subshell, so the
   # caller must check the return value.
-  echo "Command $@ did not die within $wait_time seconds"
+  echo "Command $* did not die within $wait_time seconds"
   return 1
 }
 
diff --git a/src/test/shell/testenv.sh b/src/test/shell/testenv.sh
index 7594e53..b8bc2e0 100755
--- a/src/test/shell/testenv.sh
+++ b/src/test/shell/testenv.sh
@@ -37,7 +37,7 @@
 function _log_base() {
   prefix=$1
   shift
-  echo >&2 "${prefix}[$(basename "$0") $(date "+%H:%M:%S.%N (%z)")] $@"
+  echo >&2 "${prefix}[$(basename "$0") $(date "+%H:%M:%S.%N (%z)")] $*"
 }
 
 function log_info() {
diff --git a/src/test/shell/unittest.bash b/src/test/shell/unittest.bash
index 73a5974..b51c2aa 100644
--- a/src/test/shell/unittest.bash
+++ b/src/test/shell/unittest.bash
@@ -517,7 +517,7 @@
       [ "$expected_item" = "$actual" ] && return 0
     done;
 
-    fail "Expected one of '${args[@]}', was '$actual'"
+    fail "Expected one of '${args[*]}', was '$actual'"
     return 1
 }
 
@@ -531,7 +531,7 @@
     unset args[last_arg_index]
     for expected_item in "${args[@]}"; do
       if [ "$expected_item" = "$actual" ]; then
-        fail "'${args[@]}' contains '$actual'"
+        fail "'${args[*]}' contains '$actual'"
         return 1
       fi
     done;
diff --git a/src/tools/runfiles/runfiles_posix_test.sh b/src/tools/runfiles/runfiles_posix_test.sh
index 794c5fe..9a90661 100755
--- a/src/tools/runfiles/runfiles_posix_test.sh
+++ b/src/tools/runfiles/runfiles_posix_test.sh
@@ -19,7 +19,7 @@
 function _log_base() {
   prefix=$1
   shift
-  echo >&2 "${prefix}[$(basename "$0") $(date "+%H:%M:%S.%N (%z)")] $@"
+  echo >&2 "${prefix}[$(basename "$0") $(date "+%H:%M:%S.%N (%z)")] $*"
 }
 
 function log_fatal() {
diff --git a/src/tools/runfiles/runfiles_windows_test.sh b/src/tools/runfiles/runfiles_windows_test.sh
old mode 100644
new mode 100755
index c225493..3a76c72
--- a/src/tools/runfiles/runfiles_windows_test.sh
+++ b/src/tools/runfiles/runfiles_windows_test.sh
@@ -24,7 +24,7 @@
 function _log_base() {
   prefix=$1
   shift
-  echo >&2 "${prefix}[$(basename "$0") $(date "+%H:%M:%S.%N (%z)")] $@"
+  echo >&2 "${prefix}[$(basename "$0") $(date "+%H:%M:%S.%N (%z)")] $*"
 }
 
 function log_fatal() {