Environment and PATH
PATH Order
Search Directories Left to Right
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.
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"
tool_dir ← /opt/app/bin
3tool_dir="/opt/app/bin"4PATH="$tool_dir:/usr/bin"values this step/opt/app/bintool_dirPATH ← /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_dirdeploy_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_direcho "$deploy_path"
5deploy_path="$tool_dir/deploy"6echo "$deploy_path"output/opt/app/bin/deployvalues this step/opt/app/bin/deploydeploy_path
tool_dir ← /tmp/tools
3tool_dir="/tmp/tools"4PATH="$tool_dir:/usr/bin"values this step/tmp/toolstool_dirPATH ← /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_dirdeploy_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_direcho "$deploy_path"
5deploy_path="$tool_dir/deploy"6echo "$deploy_path"output/tmp/tools/deployvalues this step/tmp/tools/deploydeploy_path
tool_dir ← /usr/local/team
3tool_dir="/usr/local/team"4PATH="$tool_dir:/usr/bin"values this step/usr/local/teamtool_dirPATH ← /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_dirdeploy_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_direcho "$deploy_path"
5deploy_path="$tool_dir/deploy"6echo "$deploy_path"output/usr/local/team/deployvalues 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.