Skip to content

skill-shell-script-hardcoded-paths

Shell script contains hardcoded absolute paths that reduce portability

Warning

Rule Details

Hardcoded absolute paths like /Users/john/project or /home/deploy/app make shell scripts non-portable across different systems, users, and environments. This rule scans .sh and .bash files for paths under /Users, /home, /opt, /var, /etc, and /usr/local. Standard system paths like /dev/null, /tmp, /usr/bin/env, and shell paths are excluded. Comments and shebang lines are also skipped. Only one warning per file is reported.

Incorrect

Script with hardcoded home directory path

bash
#!/bin/bash
set -euo pipefail
CONFIG="/Users/john/project/.config"

Script referencing a hardcoded /opt path

bash
#!/bin/bash
set -euo pipefail
source /opt/mycompany/scripts/common.sh

Correct

Script using environment variables for paths

bash
#!/bin/bash
set -euo pipefail
CONFIG="$HOME/project/.config"

Script using relative paths

bash
#!/bin/bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "$SCRIPT_DIR/common.sh"

How To Fix

Replace hardcoded absolute paths with environment variables like $HOME, $PWD, or $(dirname "$0") for script-relative paths. Use relative paths when possible.

Options

This rule does not have any configuration options.

Resources

Version

Available since: v0.2.0