fix(shell): use [[ instead of [ for conditional tests

Replace single brackets with double brackets in bash conditional
expressions across 14 files (28 changes). All scripts use bash
shebangs so [[ is safe everywhere (SonarCloud rule shelldre:S7688).
This commit is contained in:
2026-02-07 13:59:08 +02:00
parent 89aeb29c04
commit 63a21d08b4
14 changed files with 27 additions and 27 deletions

View File

@@ -54,7 +54,7 @@ remove_old_submodule() {
local name="$1" path="$2" local name="$1" path="$2"
# Remove working tree # Remove working tree
if [ -d "$path" ]; then if [[ -d "$path" ]]; then
rm -rf "$path" rm -rf "$path"
_log "Removed $path" _log "Removed $path"
fi fi
@@ -66,13 +66,13 @@ remove_old_submodule() {
git config --remove-section "submodule.$path" 2> /dev/null || true git config --remove-section "submodule.$path" 2> /dev/null || true
# Skip name-based cleanup if no submodule name provided # Skip name-based cleanup if no submodule name provided
[ -z "$name" ] && return 0 [[ -z "$name" ]] && return 0
# Remove .git/config section keyed by name # Remove .git/config section keyed by name
git config --remove-section "submodule.$name" 2> /dev/null || true git config --remove-section "submodule.$name" 2> /dev/null || true
# Remove .git/modules/<name>/ cached repository # Remove .git/modules/<name>/ cached repository
if [ -d ".git/modules/$name" ]; then if [[ -d ".git/modules/$name" ]]; then
rm -rf ".git/modules/$name" rm -rf ".git/modules/$name"
_log "Removed .git/modules/$name" _log "Removed .git/modules/$name"
fi fi

View File

@@ -1,4 +1,4 @@
#!/bin/env bash #!/bin/env bash
[ -z "$NVM_DIR" ] && export NVM_DIR="$HOME/.config/nvm" [[ -z "$NVM_DIR" ]] && export NVM_DIR="$HOME/.config/nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [[ -s "$NVM_DIR/nvm.sh" ]] && \. "$NVM_DIR/nvm.sh" # This loads nvm

View File

@@ -5,7 +5,7 @@
# shellcheck shell=bash # shellcheck shell=bash
# Defaults # Defaults
[ -z "$DOTFILES" ] && export DOTFILES="$HOME/.dotfiles" [[ -z "$DOTFILES" ]] && export DOTFILES="$HOME/.dotfiles"
DOTFILES_CURRENT_SHELL=$(basename "$SHELL") DOTFILES_CURRENT_SHELL=$(basename "$SHELL")
export DOTFILES_CURRENT_SHELL export DOTFILES_CURRENT_SHELL
@@ -15,7 +15,7 @@ VERBOSE="${VERBOSE:-0}"
DEBUG="${DEBUG:-0}" DEBUG="${DEBUG:-0}"
# Enable debugging with DEBUG=1 # Enable debugging with DEBUG=1
[ "${DEBUG:-0}" -eq 1 ] && set -x [[ "${DEBUG:-0}" -eq 1 ]] && set -x
# Detect the current shell # Detect the current shell
CURRENT_SHELL=$(ps -p $$ -ocomm= | awk -F/ '{print $NF}') CURRENT_SHELL=$(ps -p $$ -ocomm= | awk -F/ '{print $NF}')
@@ -74,7 +74,7 @@ x-path-prepend "$DOTFILES/local/bin"
x-path-prepend "$XDG_BIN_HOME" x-path-prepend "$XDG_BIN_HOME"
# Custom completion paths # Custom completion paths
[ -z "$ZSH_CUSTOM_COMPLETION_PATH" ] && export ZSH_CUSTOM_COMPLETION_PATH="$XDG_CONFIG_HOME/zsh/completion" [[ -z "$ZSH_CUSTOM_COMPLETION_PATH" ]] && export ZSH_CUSTOM_COMPLETION_PATH="$XDG_CONFIG_HOME/zsh/completion"
x-dc "$ZSH_CUSTOM_COMPLETION_PATH" x-dc "$ZSH_CUSTOM_COMPLETION_PATH"
export FPATH="$ZSH_CUSTOM_COMPLETION_PATH:$FPATH" export FPATH="$ZSH_CUSTOM_COMPLETION_PATH:$FPATH"
@@ -83,7 +83,7 @@ if ! declare -f msg > /dev/null; then
# $1 - message (string) # $1 - message (string)
msg() msg()
{ {
[ "$VERBOSE" -eq 1 ] && msgr msg "$1" [[ "$VERBOSE" -eq 1 ]] && msgr msg "$1"
return 0 return 0
} }
msg "msg was not defined, defined it now" msg "msg was not defined, defined it now"

View File

@@ -7,13 +7,13 @@ DEFAULT_NAME="main"
CURRENT_SESSION=$(tmux display-message -p "#{session_name}") CURRENT_SESSION=$(tmux display-message -p "#{session_name}")
# Check that the session has a name # Check that the session has a name
if [ "$CURRENT_SESSION" = "#{session_name}" ] || [ "$CURRENT_SESSION" = "0" ]; then if [[ "$CURRENT_SESSION" = "#{session_name}" ]] || [[ "$CURRENT_SESSION" = "0" ]]; then
# Check if the default name is already in use # Check if the default name is already in use
if tmux has-session -t "$DEFAULT_NAME" 2> /dev/null; then if tmux has-session -t "$DEFAULT_NAME" 2> /dev/null; then
# Query the user for a new name # Query the user for a new name
echo "Session name '$DEFAULT_NAME' is already in use. Enter a new name:" echo "Session name '$DEFAULT_NAME' is already in use. Enter a new name:"
read -r NEW_NAME read -r NEW_NAME
while tmux has-session -t "$NEW_NAME" 2> /dev/null || [ -z "$NEW_NAME" ]; do while tmux has-session -t "$NEW_NAME" 2> /dev/null || [[ -z "$NEW_NAME" ]]; do
echo "Name '$NEW_NAME' is invalid or already in use. Enter a new name:" echo "Name '$NEW_NAME' is invalid or already in use. Enter a new name:"
read -r NEW_NAME read -r NEW_NAME
done done

View File

@@ -24,7 +24,7 @@ check_required_tools()
clone_or_update_repo() clone_or_update_repo()
{ {
if [ ! -d "$PBB_TEMP_DIR/.git" ]; then if [[ ! -d "$PBB_TEMP_DIR/.git" ]]; then
msg_run "Starting to clone $PBB_GIT" msg_run "Starting to clone $PBB_GIT"
git clone --depth 1 --single-branch -q "$PBB_GIT" "$PBB_TEMP_DIR" \ git clone --depth 1 --single-branch -q "$PBB_GIT" "$PBB_TEMP_DIR" \
&& msg_yay "Cloned $PBB_GIT" && msg_yay "Cloned $PBB_GIT"
@@ -41,7 +41,7 @@ prepare_cheat_dest()
local cheat_dest local cheat_dest
cheat_dest="$(cheat -d | grep pure-bash-bible | head -1 | awk '{print $2}')" cheat_dest="$(cheat -d | grep pure-bash-bible | head -1 | awk '{print $2}')"
if [ ! -d "$cheat_dest" ]; then if [[ ! -d "$cheat_dest" ]]; then
mkdir -p "$cheat_dest" mkdir -p "$cheat_dest"
fi fi
@@ -83,7 +83,7 @@ process_chapters()
LC_ALL=C perl -pi.bak -e 's/\<\!-- CHAPTER END --\>//' "$cheat_file" LC_ALL=C perl -pi.bak -e 's/\<\!-- CHAPTER END --\>//' "$cheat_file"
rm "$cheat_file.bak" rm "$cheat_file.bak"
if [ '---' != "$(head -1 < "$cheat_file")" ]; then if [[ '---' != "$(head -1 < "$cheat_file")" ]]; then
local metadata local metadata
metadata="$PBB_SYNTAX\n$PBB_TAGS\n$PBB_SOURCE\n" metadata="$PBB_SYNTAX\n$PBB_TAGS\n$PBB_SOURCE\n"
printf '%s\n%b%s\n%s' "---" "$metadata" "---" "$(cat "$cheat_file")" > "$cheat_file" printf '%s\n%b%s\n%s' "---" "$metadata" "---" "$(cat "$cheat_file")" > "$cheat_file"

View File

@@ -14,7 +14,7 @@ EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "p
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")" ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]; then if [[ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]]; then
echo >&2 'ERROR: Invalid installer checksum' echo >&2 'ERROR: Invalid installer checksum'
rm composer-setup.php rm composer-setup.php
exit 1 exit 1
@@ -23,7 +23,7 @@ fi
php composer-setup.php --quiet php composer-setup.php --quiet
RESULT=$? RESULT=$?
rm composer-setup.php rm composer-setup.php
if [ $RESULT -eq 0 ]; then if [[ $RESULT -eq 0 ]]; then
mv composer.phar ~/.local/bin/composer mv composer.phar ~/.local/bin/composer
fi fi
exit $RESULT exit $RESULT

View File

@@ -18,7 +18,7 @@ fonts=(
# Function to clone or update the NerdFonts repository # Function to clone or update the NerdFonts repository
clone_or_update_repo() clone_or_update_repo()
{ {
if [ ! -d "$TMP_PATH" ]; then if [[ ! -d "$TMP_PATH" ]]; then
git clone --quiet --filter=blob:none --sparse --depth=1 "$GIT_REPO" "$TMP_PATH" git clone --quiet --filter=blob:none --sparse --depth=1 "$GIT_REPO" "$TMP_PATH"
fi fi

View File

@@ -15,7 +15,7 @@ if ! command -v git-crypt &> /dev/null; then
BUILD_PATH="$(mktemp -d)" BUILD_PATH="$(mktemp -d)"
trap 'rm -rf "$BUILD_PATH"' EXIT trap 'rm -rf "$BUILD_PATH"' EXIT
if [ ! -f "$CHECK_PATH" ]; then if [[ ! -f "$CHECK_PATH" ]]; then
git clone --depth 1 "$REPO_URL" "$BUILD_PATH" || { msgr err "Failed to clone $REPO_URL"; exit 1; } git clone --depth 1 "$REPO_URL" "$BUILD_PATH" || { msgr err "Failed to clone $REPO_URL"; exit 1; }
cd "$BUILD_PATH" || { msgr err "$BUILD_PATH not found"; exit 1; } cd "$BUILD_PATH" || { msgr err "$BUILD_PATH not found"; exit 1; }
make && make install PREFIX="$HOME/.local" make && make install PREFIX="$HOME/.local"

View File

@@ -5,7 +5,7 @@ set -uo pipefail
# This script contains large portions from following scripts: # This script contains large portions from following scripts:
# - https://github.com/freekmurze/dotfiles/blob/main/macos/set-defaults.sh # - https://github.com/freekmurze/dotfiles/blob/main/macos/set-defaults.sh
[ "$(uname)" != "Darwin" ] && echo "Not a macOS system" && exit 0 [[ "$(uname)" != "Darwin" ]] && echo "Not a macOS system" && exit 0
# shellcheck source=shared.sh # shellcheck source=shared.sh
source "$DOTFILES/config/shared.sh" source "$DOTFILES/config/shared.sh"

View File

@@ -43,7 +43,7 @@ install_ntfy()
mkdir -p ~/.config/ntfy mkdir -p ~/.config/ntfy
# Copy config only if it does not exist # Copy config only if it does not exist
if [ ! -f "$HOME/.config/ntfy/client.yml" ]; then if [[ ! -f "$HOME/.config/ntfy/client.yml" ]]; then
cp "$tmpdir/${NTFY_DIR}/client/client.yml" ~/.config/ntfy/client.yml cp "$tmpdir/${NTFY_DIR}/client/client.yml" ~/.config/ntfy/client.yml
fi fi
} }

View File

@@ -5,7 +5,7 @@ set -euo pipefail
# #
# Check if the script is running on macOS # Check if the script is running on macOS
if [ "$(uname)" != "Darwin" ]; then if [[ "$(uname)" != "Darwin" ]]; then
msgr warn "Not a macOS system" msgr warn "Not a macOS system"
exit 0 exit 0
fi fi
@@ -40,7 +40,7 @@ prompt_xcode_install()
'tell app "System Events" to display dialog "Please click install when Command Line Developer Tools appears"' 'tell app "System Events" to display dialog "Please click install when Command Line Developer Tools appears"'
)" )"
if [ "$XCODE_MESSAGE" = "button returned:OK" ]; then if [[ "$XCODE_MESSAGE" = "button returned:OK" ]]; then
xcode-select --install xcode-select --install
else else
msgr warn "You have cancelled the installation, please rerun the installer." msgr warn "You have cancelled the installation, please rerun the installer."
@@ -53,13 +53,13 @@ main()
{ {
keep_alive_sudo keep_alive_sudo
if [ -x "$XCODE_SWIFT_PATH" ]; then if [[ -x "$XCODE_SWIFT_PATH" ]]; then
msgr run "You have swift from xcode-select. Continuing..." msgr run "You have swift from xcode-select. Continuing..."
else else
prompt_xcode_install prompt_xcode_install
fi fi
until [ -f "$XCODE_SWIFT_PATH" ]; do until [[ -f "$XCODE_SWIFT_PATH" ]]; do
echo -n "." echo -n "."
sleep 1 sleep 1
done done

View File

@@ -14,7 +14,7 @@ clone_z_repo()
local git_path=$1 local git_path=$1
local bin_path=$2 local bin_path=$2
if [ ! -d "$bin_path" ]; then if [[ ! -d "$bin_path" ]]; then
git clone "$git_path" "$bin_path" git clone "$git_path" "$bin_path"
msgr run_done "z installed at $bin_path" msgr run_done "z installed at $bin_path"
else else

View File

@@ -5,7 +5,7 @@
: "${VERBOSE:=0}" : "${VERBOSE:=0}"
# Source the main shared config if not already loaded # Source the main shared config if not already loaded
if [ -z "${SHARED_SCRIPTS_SOURCED:-}" ]; then if [[ -z "${SHARED_SCRIPTS_SOURCED:-}" ]]; then
source "${DOTFILES}/config/shared.sh" source "${DOTFILES}/config/shared.sh"
export SHARED_SCRIPTS_SOURCED=1 export SHARED_SCRIPTS_SOURCED=1
fi fi

View File

@@ -3,7 +3,7 @@
set -euo pipefail set -euo pipefail
if [ -x "node_modules/bats/bin/bats" ]; then if [[ -x "node_modules/bats/bin/bats" ]]; then
git ls-files '*.bats' -z | xargs -0 node_modules/bats/bin/bats git ls-files '*.bats' -z | xargs -0 node_modules/bats/bin/bats
elif command -v npx > /dev/null; then elif command -v npx > /dev/null; then
git ls-files '*.bats' -z | xargs -0 npx --yes bats git ls-files '*.bats' -z | xargs -0 npx --yes bats