Various Shell Script Fixes and Improvements - Part One

see #4023

Closes #4051.

PiperOrigin-RevId: 177279457
diff --git a/scripts/bash_completion_test.sh b/scripts/bash_completion_test.sh
index 3bf7999..adb75f7 100755
--- a/scripts/bash_completion_test.sh
+++ b/scripts/bash_completion_test.sh
@@ -68,7 +68,7 @@
         #
         # Alias expansion still inserts an extra space after 'blaze',
         # though, hence the following sed.  Not sure why.
-        for i in ${COMMAND_ALIASES[@]}; do
+        for i in "${COMMAND_ALIASES[@]}"; do
           echo "alias $i=\"echo $i'\""
         done
         echo -en "$input'"
@@ -82,7 +82,7 @@
 # e.g. assert_expansion 'foo' 'foo_expand' 'flag1=bar;flag2=baz'
 assert_expansion() {
     local prefix=$1 expected=$2 flags=${3:-}
-    for i in ${COMMAND_ALIASES[@]}; do
+    for i in "${COMMAND_ALIASES[@]}"; do
       local nprefix="$i $prefix"
       local nexpected="$i $expected"
       assert_equals "$nexpected" "$(expand "$nprefix\t" "$flags" "/dev/null")"
@@ -100,7 +100,7 @@
 assert_expansion_error_not_contains() {
   local prefix=$1 not_expected=$2 flags=${3:-}
   local temp_file="$(mktemp "${TEST_TMPDIR}/tmp.stderr.XXXXXX")"
-  for i in ${COMMAND_ALIASES[@]}; do
+  for i in "${COMMAND_ALIASES[@]}"; do
     local nprefix="$i "
     expand "$nprefix\t" "$flags" "$temp_file" > /dev/null
     assert_not_contains "$not_expected" "$temp_file"
diff --git a/scripts/bootstrap/compile.sh b/scripts/bootstrap/compile.sh
index a12b2e3..cd05bad 100755
--- a/scripts/bootstrap/compile.sh
+++ b/scripts/bootstrap/compile.sh
@@ -20,7 +20,7 @@
 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" " ")
-GUAVA_VERSIONE=23.1
+GUAVA_VERSION=23.1
 GUAVA_JARS=$(find third_party/guava -name '*.jar' | grep -e .*${GUAVA_VERSION}.*jar | tr "\n" " ")
 LIBRARY_JARS="${LIBRARY_JARS} ${GRPC_LIBRARY_JARS} ${GUAVA_JARS}"
 
@@ -41,7 +41,7 @@
   TEMP_FOR_SWAP="${LIBRARY_JARS_ARRAY[$ERROR_PRONE_INDEX]}"
   LIBRARY_JARS_ARRAY[$ERROR_PRONE_INDEX]="${LIBRARY_JARS_ARRAY[$GUAVA_INDEX]}"
   LIBRARY_JARS_ARRAY[$GUAVA_INDEX]="$TEMP_FOR_SWAP"
-  LIBRARY_JARS="${LIBRARY_JARS_ARRAY[@]}"
+  LIBRARY_JARS="${LIBRARY_JARS_ARRAY[*]}"
 fi
 
 DIRS=$(echo src/{java_tools/singlejar/java/com/google/devtools/build/zip,main/java,tools/xcode-common/java/com/google/devtools/build/xcode/{common,util}} third_party/java/dd_plist/java ${OUTPUT_DIR}/src)
diff --git a/scripts/release/release.sh b/scripts/release/release.sh
index 6a2559a..3b18fd8 100755
--- a/scripts/release/release.sh
+++ b/scripts/release/release.sh
@@ -122,11 +122,11 @@
 function apply_cherry_picks() {
   echo "Applying cherry-picks"
   # Apply cherry-picks
-  for i in $@; do
+  for commit in "$@"; do
     local previous_head="$(git rev-parse HEAD)"
-    echo "  Cherry-picking $i"
-    git cherry-pick $i >/dev/null || {
-      echo "Failed to cherry-pick $i. please resolve the conflict and exit." >&2
+    echo "  Cherry-picking ${commit}"
+    git cherry-pick ${commit} >/dev/null || {
+      echo "Failed to cherry-pick ${commit}. please resolve the conflict and exit." >&2
       echo "  Use 'git cherry-pick --abort; exit' to abort the cherry-picks." >&2
       echo "  Use 'git cherry-pick --continue; exit' to resolve the conflict." >&2
       bash
@@ -137,7 +137,7 @@
     }
     # Add the origin of the cherry-pick in case the patch-id diverge and we cannot
     # find the original commit.
-    git notes --ref=cherrypick add -f -m "$i"
+    git notes --ref=cherrypick add -f -m "${commit}"
   done
   return 0
 }
@@ -147,9 +147,9 @@
   local branch="${1:-HEAD}"
   local baseline="${2:-$(get_release_baseline "${branch}")}"
   local changes="$(git log --pretty=format:%H "${baseline}~".."${branch}")"
-  for i in ${changes}; do
-    if git notes --ref=release show $i &>/dev/null; then
-      echo $i
+  for change in ${changes}; do
+    if git notes --ref=release show ${change} &>/dev/null; then
+      echo ${change}
       return 0
     fi
   done
diff --git a/scripts/release/relnotes.sh b/scripts/release/relnotes.sh
index 94edb04..d88ef46 100755
--- a/scripts/release/relnotes.sh
+++ b/scripts/release/relnotes.sh
@@ -100,8 +100,8 @@
   for i in "${RELNOTES_TYPES[@]}"; do
     eval "RELNOTES_${i}=()"
   done
-  for i in $@; do
-    extract_release_note $i
+  for commit in $@; do
+    extract_release_note "${commit}"
   done
 }
 
diff --git a/src/test/shell/bazel/workspace_test.sh b/src/test/shell/bazel/workspace_test.sh
index bd436ef..d09e37a 100755
--- a/src/test/shell/bazel/workspace_test.sh
+++ b/src/test/shell/bazel/workspace_test.sh
@@ -104,7 +104,7 @@
 EOF
   bazel fetch //:test || fail "Fetch failed"
   bazel build //:test || echo "Expected build to succeed"
-  check_eq "12" "$(cat bazel-genfiles/test.out | tr -d '[[:space:]]')"
+  check_eq "12" "$(cat bazel-genfiles/test.out | tr -d '[:space:]')"
 }
 
 # Regression test for issue #724: NullPointerException in WorkspaceFile
diff --git a/src/test/shell/integration/java_integration_test.sh b/src/test/shell/integration/java_integration_test.sh
index 984eca7..f2f6933 100755
--- a/src/test/shell/integration/java_integration_test.sh
+++ b/src/test/shell/integration/java_integration_test.sh
@@ -270,7 +270,7 @@
   cp ${PRODUCT_NAME}-bin/$pkg/java/hello/hello_deploy.jar $pkg/ugly/
 
   $pkg/ugly/hello build.target build.time build.timestamp \
-      main.class=hello.Hello "$expected_build_data" 2>&1 >>$TEST_log
+      main.class=hello.Hello "$expected_build_data" >> $TEST_log 2>&1
   expect_log 'Hello, World!'
 }
 
diff --git a/src/test/shell/integration/loading_phase_tests.sh b/src/test/shell/integration/loading_phase_tests.sh
index 94bbd04..4227fe1 100755
--- a/src/test/shell/integration/loading_phase_tests.sh
+++ b/src/test/shell/integration/loading_phase_tests.sh
@@ -131,7 +131,7 @@
               awk '{print $1}') \
           startup_options \
           target-syntax)
-  for topic in ${topics[@]}; do
+  for topic in "${topics[@]}"; do
     bazel help $topic >$TEST_log 2>&1 || {
        fail "help $topic failed"
        expect_not_log .  # print the log
diff --git a/src/test/shell/integration/progress_reporting_test.sh b/src/test/shell/integration/progress_reporting_test.sh
index 6027b5a..f011994 100755
--- a/src/test/shell/integration/progress_reporting_test.sh
+++ b/src/test/shell/integration/progress_reporting_test.sh
@@ -25,7 +25,7 @@
 
 # TODO(b/37617303): make tests UI-independent
 add_to_bazelrc "build --noexperimental_ui"
-add_to_bazelrc "build --workspace_status_command="$(which true)" --nostamp"
+add_to_bazelrc "build --workspace_status_command=$(which true) --nostamp"
 add_to_bazelrc "build --show_progress_rate_limit=-1"
 add_to_bazelrc "build --genrule_strategy=local"
 
diff --git a/src/test/shell/integration_test_setup.sh b/src/test/shell/integration_test_setup.sh
index 73f7c92..ba051d4 100755
--- a/src/test/shell/integration_test_setup.sh
+++ b/src/test/shell/integration_test_setup.sh
@@ -22,7 +22,7 @@
 CURRENT_SCRIPT=${BASH_SOURCE[0]}
 # Go to the directory where the script is running
 cd "$(dirname ${CURRENT_SCRIPT})" \
-  || print_message_and_exit "Unable to access "$(dirname ${CURRENT_SCRIPT})""
+  || print_message_and_exit "Unable to access $(dirname ${CURRENT_SCRIPT})"
 
 DIR=$(pwd)
 # Load the unit test framework
diff --git a/src/test/shell/testenv.sh b/src/test/shell/testenv.sh
index e9eee33..20f5094 100755
--- a/src/test/shell/testenv.sh
+++ b/src/test/shell/testenv.sh
@@ -472,7 +472,7 @@
     cd ${WORKSPACE_DIR}
     bazel clean >> $TEST_log 2>&1 # Clean up the output base
 
-    for i in $(ls); do
+    for i in *; do
       if ! is_tools_directory "$i"; then
         rm -fr "$i"
       fi
diff --git a/src/tools/xcode/actoolwrapper/actoolwrapper.sh b/src/tools/xcode/actoolwrapper/actoolwrapper.sh
index 1a55ac1..ddf7e43 100755
--- a/src/tools/xcode/actoolwrapper/actoolwrapper.sh
+++ b/src/tools/xcode/actoolwrapper/actoolwrapper.sh
@@ -40,7 +40,7 @@
 
 TOOLARGS=()
 LASTARG=""
-for i in $@; do
+for i in "$@"; do
   if [ "$LASTARG" = "--output-partial-info-plist" ]; then
     touch "$i"
   fi
diff --git a/src/tools/xcode/environment/environment_plist.sh b/src/tools/xcode/environment/environment_plist.sh
index 057a9c8..c1b5d86 100755
--- a/src/tools/xcode/environment/environment_plist.sh
+++ b/src/tools/xcode/environment/environment_plist.sh
@@ -25,7 +25,7 @@
 
 set -eu
 
-while [[ $# > 1 ]]
+while [[ $# -gt 1 ]]
 do
 key="$1"
 
diff --git a/src/tools/xcode/ibtoolwrapper/ibtoolwrapper.sh b/src/tools/xcode/ibtoolwrapper/ibtoolwrapper.sh
index be72527..ca02674 100755
--- a/src/tools/xcode/ibtoolwrapper/ibtoolwrapper.sh
+++ b/src/tools/xcode/ibtoolwrapper/ibtoolwrapper.sh
@@ -56,7 +56,7 @@
 # By default, have ibtool compile storyboards (to stay compatible with the
 # native rules). If the command line includes "--link", we use it instead.
 ACTION=--compile
-for i in $@; do
+for i in "$@"; do
   if [ -e "$i" ]; then
     if [[ "$i" == *.zip ]]; then
       unzip -qq "$i" -d "$LINKDIR"
diff --git a/tools/genrule/genrule-setup.sh b/tools/genrule/genrule-setup.sh
index 2fe352f..98d68f1 100755
--- a/tools/genrule/genrule-setup.sh
+++ b/tools/genrule/genrule-setup.sh
@@ -1,3 +1,5 @@
+#!/bin/bash
+
 # Copyright 2017 The Bazel Authors. All rights reserved.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");