[shellcheck] fix SC2155 warning (#8490)

Declare and assign separately to avoid masking return values.
diff --git a/script/check-api-version b/script/check-api-version
index 1eb58e2..4f3f8bd 100755
--- a/script/check-api-version
+++ b/script/check-api-version
@@ -29,8 +29,11 @@
 
 set -euo pipefail
 
-readonly OT_SHA_OLD="$(git cat-file -p HEAD | grep 'parent ' | head -n1 | cut -d' ' -f2)"
-readonly OT_VERSIONS_FILE=tmp/api_versions
+OT_SHA_OLD="$(git cat-file -p HEAD | grep 'parent ' | head -n1 | cut -d' ' -f2)"
+readonly OT_SHA_OLD
+
+OT_VERSIONS_FILE=tmp/api_versions
+readonly OT_VERSIONS_FILE
 
 die()
 {
diff --git a/script/check-arm-build b/script/check-arm-build
index 75b3660..955ccc8 100755
--- a/script/check-arm-build
+++ b/script/check-arm-build
@@ -29,8 +29,11 @@
 
 set -euxo pipefail
 
-readonly OT_TMP_DIR=/tmp/ot-arm-build-cmake
-readonly OT_SHA_NEW=${GITHUB_SHA:-$(git rev-parse HEAD)}
+OT_TMP_DIR=/tmp/ot-arm-build-cmake
+readonly OT_TMP_DIR
+
+OT_SHA_NEW=${GITHUB_SHA:-$(git rev-parse HEAD)}
+readonly OT_SHA_NEW
 
 build_nrf52840()
 {
diff --git a/script/check-posix-build-cmake b/script/check-posix-build-cmake
index cf9a4f5..90dc898 100755
--- a/script/check-posix-build-cmake
+++ b/script/check-posix-build-cmake
@@ -29,7 +29,8 @@
 
 set -euxo pipefail
 
-readonly OT_BUILDDIR="$(pwd)/build"
+OT_BUILDDIR="$(pwd)/build"
+readonly OT_BUILDDIR
 
 reset_source()
 {
diff --git a/script/check-posix-pty b/script/check-posix-pty
index 716d154..0cb7939 100755
--- a/script/check-posix-pty
+++ b/script/check-posix-pty
@@ -112,7 +112,8 @@
     $RADIO_NCP_PATH 1 >"$RADIO_PTY" <"$RADIO_PTY" &
 
     # Cover setting a valid network interface name.
-    readonly VALID_NETIF_NAME="wan$(date +%H%M%S)"
+    VALID_NETIF_NAME="wan$(date +%H%M%S)"
+    readonly VALID_NETIF_NAME
 
     RADIO_URL="spinel+hdlc+uart://${CORE_PTY}?region=US&max-power-table=11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26"
 
@@ -140,7 +141,8 @@
         # sleep a while for daemon ready
         sleep 2
 
-        readonly OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH=640
+        OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH=640
+        readonly OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH
         local -r kMaxStringLength="$((OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH - 1))"
 
         # verify success if command length doesn't exceed the limit
@@ -160,7 +162,8 @@
 
         # Cover setting a too long(max is 15 characters) network interface name.
         # Expect exit code to be 2(OT_EXIT_INVALID_ARGUMENTS).
-        readonly INVALID_NETIF_NAME="wan0123456789123"
+        INVALID_NETIF_NAME="wan0123456789123"
+        readonly INVALID_NETIF_NAME
         sudo "${OT_CLI}" -I "${INVALID_NETIF_NAME}" -n "${RADIO_URL}" || test $? = 2
 
         OT_CLI_CMD="$PWD/build/posix/src/posix/ot-cli ${RADIO_URL}"
diff --git a/script/check-scan-build b/script/check-scan-build
index bc672d3..20064fe 100755
--- a/script/check-scan-build
+++ b/script/check-scan-build
@@ -29,9 +29,10 @@
 
 set -euxo pipefail
 
-readonly OT_SRCDIR="$(pwd)"
+OT_SRCDIR="$(pwd)"
+readonly OT_SRCDIR
 
-readonly OT_BUILD_OPTIONS=(
+OT_BUILD_OPTIONS=(
     "-DOT_ANYCAST_LOCATOR=ON"
     "-DOT_BUILD_EXECUTABLES=OFF"
     "-DOT_BORDER_AGENT=ON"
@@ -75,13 +76,15 @@
     "-DOT_SRP_CLIENT=ON"
     "-DOT_SRP_SERVER=ON"
 )
+readonly OT_BUILD_OPTIONS
 
-readonly OT_CFLAGS=(
+OT_CFLAGS=(
     "-DMBEDTLS_DEBUG_C"
     "-I$(pwd)/third_party/mbedtls"
     "-I$(pwd)/third_party/mbedtls/repo/include"
     '-DMBEDTLS_CONFIG_FILE=\"mbedtls-config.h\"'
 )
+readonly OT_CFLAGS
 
 main()
 {
diff --git a/script/check-simulation-build-autotools b/script/check-simulation-build-autotools
index bf4fcc5..5f62a1e 100755
--- a/script/check-simulation-build-autotools
+++ b/script/check-simulation-build-autotools
@@ -29,7 +29,8 @@
 
 set -euxo pipefail
 
-readonly OT_BUILD_JOBS=$(getconf _NPROCESSORS_ONLN)
+OT_BUILD_JOBS=$(getconf _NPROCESSORS_ONLN)
+readonly OT_BUILD_JOBS
 
 reset_source()
 {
diff --git a/script/check-simulation-build-cmake b/script/check-simulation-build-cmake
index 474797f..3a0800a 100755
--- a/script/check-simulation-build-cmake
+++ b/script/check-simulation-build-cmake
@@ -29,7 +29,8 @@
 
 set -euxo pipefail
 
-readonly OT_BUILDDIR="$(pwd)/build"
+OT_BUILDDIR="$(pwd)/build"
+readonly OT_BUILDDIR
 
 reset_source()
 {
diff --git a/script/check-size b/script/check-size
index e81c0bf..fef1682 100755
--- a/script/check-size
+++ b/script/check-size
@@ -29,10 +29,17 @@
 
 set -euo pipefail
 
-readonly OT_TMP_DIR=/tmp/ot-size-report
-readonly OT_SHA_NEW=${GITHUB_SHA:-$(git rev-parse HEAD)}
-readonly OT_SHA_OLD="$(git cat-file -p "${OT_SHA_NEW}" | grep 'parent ' | head -n1 | cut -d' ' -f2)"
-readonly OT_REPORT_FILE=/tmp/size_report
+OT_TMP_DIR=/tmp/ot-size-report
+readonly OT_TMP_DIR
+
+OT_SHA_NEW=${GITHUB_SHA:-$(git rev-parse HEAD)}
+readonly OT_SHA_NEW
+
+OT_SHA_OLD="$(git cat-file -p "${OT_SHA_NEW}" | grep 'parent ' | head -n1 | cut -d' ' -f2)"
+readonly OT_SHA_OLD
+
+OT_REPORT_FILE=/tmp/size_report
+readonly OT_REPORT_FILE
 
 setup_arm_gcc_7()
 {
diff --git a/script/cmake-build b/script/cmake-build
index aca7113..b445bcd 100755
--- a/script/cmake-build
+++ b/script/cmake-build
@@ -65,10 +65,12 @@
 OT_CMAKE_NINJA_TARGET=${OT_CMAKE_NINJA_TARGET:-}
 
 OT_SRCDIR="$(cd "$(dirname "$0")"/.. && pwd)"
-
 readonly OT_SRCDIR
-readonly OT_PLATFORMS=(simulation posix)
-readonly OT_POSIX_SIM_COMMON_OPTIONS=(
+
+OT_PLATFORMS=(simulation posix)
+readonly OT_PLATFORMS
+
+OT_POSIX_SIM_COMMON_OPTIONS=(
     "-DOT_ANYCAST_LOCATOR=ON"
     "-DOT_BORDER_AGENT=ON"
     "-DOT_BORDER_ROUTER=ON"
@@ -106,6 +108,7 @@
     "-DOT_RCP_RESTORATION_MAX_COUNT=2"
     "-DOT_UPTIME=ON"
 )
+readonly OT_POSIX_SIM_COMMON_OPTIONS
 
 die()
 {
diff --git a/script/gcda-tool b/script/gcda-tool
index d5bf203..1b757cc 100755
--- a/script/gcda-tool
+++ b/script/gcda-tool
@@ -29,8 +29,11 @@
 
 set -euxo pipefail
 
-readonly OT_MERGED_PROFILES=merged_profiles
-readonly OT_GCOV_PREFIX_BASE=ot-run
+OT_MERGED_PROFILES=merged_profiles
+readonly OT_MERGED_PROFILES
+
+OT_GCOV_PREFIX_BASE=ot-run
+readonly OT_GCOV_PREFIX_BASE
 
 merge_profiles()
 {
diff --git a/script/make-pretty b/script/make-pretty
index aeae031..73756af 100755
--- a/script/make-pretty
+++ b/script/make-pretty
@@ -64,15 +64,25 @@
 
 set -euo pipefail
 
-readonly OT_BUILD_JOBS=$(getconf _NPROCESSORS_ONLN)
-readonly OT_EXCLUDE_DIRS=(third_party doc/site)
+OT_BUILD_JOBS=$(getconf _NPROCESSORS_ONLN)
+readonly OT_BUILD_JOBS
 
-readonly OT_CLANG_SOURCES=('*.c' '*.cc' '*.cpp' '*.h' '*.hpp')
-readonly OT_MARKDOWN_SOURCES=('*.md')
-readonly OT_PYTHON_SOURCES=('*.py')
+OT_EXCLUDE_DIRS=(third_party doc/site)
+readonly OT_EXCLUDE_DIRS
 
-readonly OT_CLANG_TIDY_FIX_DIRS=('examples' 'include' 'src' 'tests')
-readonly OT_CLANG_TIDY_BUILD_OPTS=(
+OT_CLANG_SOURCES=('*.c' '*.cc' '*.cpp' '*.h' '*.hpp')
+readonly OT_CLANG_SOURCES
+
+OT_MARKDOWN_SOURCES=('*.md')
+readonly OT_MARKDOWN_SOURCES
+
+OT_PYTHON_SOURCES=('*.py')
+readonly OT_PYTHON_SOURCES
+
+OT_CLANG_TIDY_FIX_DIRS=('examples' 'include' 'src' 'tests')
+readonly OT_CLANG_TIDY_FIX_DIRS
+
+OT_CLANG_TIDY_BUILD_OPTS=(
     '-DCMAKE_EXPORT_COMPILE_COMMANDS=ON'
     '-DOT_ANYCAST_LOCATOR=ON'
     '-DOT_APP_RCP=OFF'
@@ -129,8 +139,9 @@
     '-DOT_COMPILE_WARNING_AS_ERROR=ON'
     '-DOT_UPTIME=ON'
 )
+readonly OT_CLANG_TIDY_BUILD_OPTS
 
-readonly OT_CLANG_TIDY_CHECKS="\
+OT_CLANG_TIDY_CHECKS="\
 -*,\
 google-explicit-constructor,\
 google-readability-casting,\
@@ -149,8 +160,7 @@
 readability-simplify-boolean-expr,\
 readability-static-accessed-through-instance,\
 "
-
-#performance-for-range-copy\
+readonly OT_CLANG_TIDY_CHECKS
 
 do_clang_format()
 {
diff --git a/script/package b/script/package
index 4eaafdf..65df10b 100755
--- a/script/package
+++ b/script/package
@@ -32,7 +32,8 @@
 
 set -euo pipefail
 
-readonly OT_BUILDDIR="${OT_BUILDDIR:-${PWD}/build}"
+OT_BUILDDIR="${OT_BUILDDIR:-${PWD}/build}"
+readonly OT_BUILDDIR
 
 main()
 {
diff --git a/script/test b/script/test
index 4815df9..30eb4c0 100755
--- a/script/test
+++ b/script/test
@@ -32,28 +32,62 @@
 
 set -euo pipefail
 
-readonly OT_BUILDDIR="${OT_BUILDDIR:-${PWD}/build}"
-readonly OT_SRCDIR="${PWD}"
+OT_BUILDDIR="${OT_BUILDDIR:-${PWD}/build}"
+readonly OT_BUILDDIR
 
-readonly OT_COLOR_PASS='\033[0;32m'
-readonly OT_COLOR_FAIL='\033[0;31m'
-readonly OT_COLOR_SKIP='\033[0;33m'
-readonly OT_COLOR_NONE='\033[0m'
+OT_SRCDIR="${PWD}"
+readonly OT_SRCDIR
 
-readonly OT_NODE_TYPE="${OT_NODE_TYPE:-cli}"
-readonly OT_NATIVE_IP="${OT_NATIVE_IP:-0}"
-readonly THREAD_VERSION="${THREAD_VERSION:-1.3}"
-readonly INTER_OP="${INTER_OP:-0}"
-readonly VERBOSE="${VERBOSE:-0}"
-readonly BORDER_ROUTING="${BORDER_ROUTING:-1}"
-readonly NAT64="${NAT64:-0}"
-readonly NAT64_SERVICE="${NAT64_SERVICE:-openthread}"
-readonly INTER_OP_BBR="${INTER_OP_BBR:-1}"
+OT_COLOR_PASS='\033[0;32m'
+readonly OT_COLOR_PASS
 
-readonly OT_COREDUMP_DIR="${PWD}/ot-core-dump"
-readonly FULL_LOGS=${FULL_LOGS:-0}
-readonly TREL=${TREL:-0}
-readonly LOCAL_OTBR_DIR=${LOCAL_OTBR_DIR:-""}
+OT_COLOR_FAIL='\033[0;31m'
+readonly OT_COLOR_FAIL
+
+OT_COLOR_SKIP='\033[0;33m'
+readonly OT_COLOR_SKIP
+
+OT_COLOR_NONE='\033[0m'
+readonly OT_COLOR_NONE
+
+OT_NODE_TYPE="${OT_NODE_TYPE:-cli}"
+readonly OT_NODE_TYPE
+
+OT_NATIVE_IP="${OT_NATIVE_IP:-0}"
+readonly OT_NATIVE_IP
+
+THREAD_VERSION="${THREAD_VERSION:-1.3}"
+readonly THREAD_VERSION
+
+INTER_OP="${INTER_OP:-0}"
+readonly INTER_OP
+
+VERBOSE="${VERBOSE:-0}"
+readonly VERBOSE
+
+BORDER_ROUTING="${BORDER_ROUTING:-1}"
+readonly BORDER_ROUTING
+
+NAT64="${NAT64:-0}"
+readonly NAT64
+
+NAT64_SERVICE="${NAT64_SERVICE:-openthread}"
+readonly NAT64_SERVICE
+
+INTER_OP_BBR="${INTER_OP_BBR:-1}"
+readonly INTER_OP_BBR
+
+OT_COREDUMP_DIR="${PWD}/ot-core-dump"
+readonly OT_COREDUMP_DIR
+
+FULL_LOGS=${FULL_LOGS:-0}
+readonly FULL_LOGS
+
+TREL=${TREL:-0}
+readonly TREL
+
+LOCAL_OTBR_DIR=${LOCAL_OTBR_DIR:-""}
+readonly LOCAL_OTBR_DIR
 
 build_simulation()
 {