Shell Bash Check PATH Environment Exist

It looks like you are trying to check if a specific directory is included in the PATH environment variable in a Bash script. The code you provided is almost correct, but it has a small issue. You can modify it as follows to make it work correctly:

1
2
3
4
5
6
7
CHECK_PATH="/root/go/bin"

if [[ ":$PATH:" == *":$CHECK_PATH:"* ]]; then
    echo "Path found in PATH environment. Skipping configuration..."
else
    echo "Path not found in PATH environment. You may need to add it."
fi

Here’s a breakdown of the changes made:

  1. Enclosed the CHECK_PATH variable in double quotes to ensure it handles paths with spaces or special characters correctly.
  2. Used double brackets [[ ... ]] for conditional testing.
  3. Added colons : before and after both PATH and CHECK_PATH to ensure accurate matching.
  4. Provided appropriate messages for both cases - when the path is found in PATH and when it’s not found.

With these modifications, the script should correctly check whether the specified path is in the PATH environment variable and provide the corresponding message.

0%