-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bash: Build new PATH the smart way #3096
Draft
ermo
wants to merge
1
commit into
main
Choose a base branch
from
build-on-existing-bash-path
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+63
−19
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,64 @@ | ||
# Begin /usr/share/defaults/etc/profile.d/10-path.sh | ||
|
||
export PATH="/usr/sbin:/usr/bin" | ||
if [ -d "/usr/local/sbin" ]; then | ||
export PATH="$PATH:/usr/local/sbin" | ||
fi | ||
PREFIX_PATH= | ||
POSTFIX_PATH= | ||
|
||
if [ -d "/usr/local/bin" ]; then | ||
export PATH="$PATH:/usr/local/bin" | ||
fi | ||
# PATH exists, only append missing stuff to it | ||
if [[ -n "${PATH}" ]]; then | ||
|
||
if [ -d "$HOME/bin" ]; then | ||
export PATH="$HOME/bin:$PATH" | ||
fi | ||
echo "DEBUG: Starting \$PATH: ${PATH}" | ||
|
||
for d in "/usr/sbin" "/usr/bin" "/usr/local/sbin" "/usr/local/bin"; do | ||
# respect user path ordering if the element is already in $PATH | ||
if [[ "${PATH}" =~ "${d}" ]]; then | ||
: | ||
echo "DEBUG: Skipping ${d} as it is already in \$PATH" | ||
else | ||
POSTFIX_PATH="${POSTFIX_PATH}:${d}" | ||
echo "DEBUG: Added ${d} to \$POSTFIX_PATH, \$POSTFIX_PATH is now: ${POSTFIX_PATH}" | ||
fi | ||
done | ||
|
||
for d in "${HOME}/.local/bin" "${HOME}/bin"; do | ||
# respect user path ordering if the element is already in $PATH | ||
if [[ "${PATH}" =~ "${d}" ]]; then | ||
: | ||
echo "DEBUG: Skipping ${d} as it is already in \$PATH" | ||
else | ||
PREFIX_PATH="${d}:${PREFIX_PATH}" | ||
echo "DEBUG: Added ${d} to \$PREFIX_PATH, \$PREFIX_PATH is now: ${PREFIX_PATH}" | ||
fi | ||
done | ||
|
||
# this should adequately respect existing path definitions | ||
PATH="${PREFIX_PATH}${PATH}${POSTFIX_PATH}" | ||
|
||
else | ||
# PATH does NOT exist, build it from scratch. | ||
|
||
if [ -d "$HOME/.local/bin" ]; then | ||
export PATH="$HOME/.local/bin:$PATH" | ||
echo "DEBUG: \$PATH is empty, building a default \$PATH ..." | ||
|
||
DEFAULT_PATH="/usr/sbin:/usr/bin" | ||
if [[ -d "/usr/local/sbin" ]]; then | ||
DEFAULT_PATH="${DEFAULT_PATH}:/usr/local/sbin" | ||
fi | ||
|
||
if [[ -d "/usr/local/bin" ]]; then | ||
DEFAULT_PATH="${DEFAULT_PATH}:/usr/local/bin" | ||
fi | ||
|
||
if [[ -d "$HOME/bin" ]]; then | ||
DEFAULT_PATH="${HOME}/bin:${DEFAULT_PATH}" | ||
fi | ||
|
||
if [[ -d "$HOME/.local/bin" ]]; then | ||
DEFAULT_PATH="${HOME}/.local/bin:${DEFAULT_PATH}" | ||
fi | ||
|
||
PATH="${DEFAULT_PATH}" | ||
fi | ||
|
||
export PATH="${PATH}" | ||
echo "DEBUG: \$PATH is now: ${PATH}" | ||
|
||
# End /usr/share/defaults/etc/profile.d/10-path.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File should start with a shebang to clarify that this is a bash script:
Additionally, please pass the file through shellcheck.