PATH is a colon-separated list of directories. Bash searches those directories from left to right when a command has no slash.

Program

Play the script to choose the first directory and build the command path Bash would find first.

tool_dir
path_order.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash

tool_dir="/opt/app/bin"
PATH="$tool_dir:/usr/bin"
deploy_path="$tool_dir/deploy"
echo "$deploy_path"
#!/usr/bin/env bash

tool_dir="/tmp/tools"
PATH="$tool_dir:/usr/bin"
deploy_path="$tool_dir/deploy"
echo "$deploy_path"
#!/usr/bin/env bash

tool_dir="/usr/local/team"
PATH="$tool_dir:/usr/bin"
deploy_path="$tool_dir/deploy"
echo "$deploy_path"
  1. tool_dir ← /opt/app/bin

    3tool_dir="/opt/app/bin"4PATH="$tool_dir:/usr/bin"
    values this step/opt/app/bintool_dir
  2. PATH ← /opt/app/bin:/usr/bin

    3tool_dir="/opt/app/bin"4PATH="$tool_dir:/usr/bin"5deploy_path="$tool_dir/deploy"
    values this step/opt/app/bin:/usr/binPATH/opt/app/bintool_dir
  3. deploy_path ← /opt/app/bin/deploy

    4PATH="$tool_dir:/usr/bin"5deploy_path="$tool_dir/deploy"6echo "$deploy_path"
    values this step/opt/app/bin/deploydeploy_path/opt/app/bintool_dir
  4. echo "$deploy_path"

    5deploy_path="$tool_dir/deploy"6echo "$deploy_path"
    output/opt/app/bin/deploy
    values this step/opt/app/bin/deploydeploy_path
  1. tool_dir ← /tmp/tools

    3tool_dir="/tmp/tools"4PATH="$tool_dir:/usr/bin"
    values this step/tmp/toolstool_dir
  2. PATH ← /tmp/tools:/usr/bin

    3tool_dir="/tmp/tools"4PATH="$tool_dir:/usr/bin"5deploy_path="$tool_dir/deploy"
    values this step/tmp/tools:/usr/binPATH/tmp/toolstool_dir
  3. deploy_path ← /tmp/tools/deploy

    4PATH="$tool_dir:/usr/bin"5deploy_path="$tool_dir/deploy"6echo "$deploy_path"
    values this step/tmp/tools/deploydeploy_path/tmp/toolstool_dir
  4. echo "$deploy_path"

    5deploy_path="$tool_dir/deploy"6echo "$deploy_path"
    output/tmp/tools/deploy
    values this step/tmp/tools/deploydeploy_path
  1. tool_dir ← /usr/local/team

    3tool_dir="/usr/local/team"4PATH="$tool_dir:/usr/bin"
    values this step/usr/local/teamtool_dir
  2. PATH ← /usr/local/team:/usr/bin

    3tool_dir="/usr/local/team"4PATH="$tool_dir:/usr/bin"5deploy_path="$tool_dir/deploy"
    values this step/usr/local/team:/usr/binPATH/usr/local/teamtool_dir
  3. deploy_path ← /usr/local/team/deploy

    4PATH="$tool_dir:/usr/bin"5deploy_path="$tool_dir/deploy"6echo "$deploy_path"
    values this step/usr/local/team/deploydeploy_path/usr/local/teamtool_dir
  4. echo "$deploy_path"

    5deploy_path="$tool_dir/deploy"6echo "$deploy_path"
    output/usr/local/team/deploy
    values this step/usr/local/team/deploydeploy_path
PATH `PATH` tells Bash where to look for commands that are not written with a slash.
search order The leftmost directory is searched first.
command path This example models the chosen path without requiring the file to exist.