3
\$\begingroup\$

I am working on improving my bash environment. I have added the following function to my ~/.bashrc in order to facilitate modifying my PATH as well as removing duplicate entries from my PATH which may occur from sourcing ~/.bashrc again.

# 1ドル the path to be added as an absolute path
# 2ドル a boolean for whether the path should be prepended. default: false
# 3ドル the path variable to add to
function addToPath {
 # ${!3} dereferences 3ドル to use the value the variable refers to. For example 3ドル=PATH would give $PATH
 if [ -z ${3+x} ]; then TMP_PATH="$PATH"; TMP_NAME="PATH"; else TMP_PATH="${!3}"; TMP_NAME="3ドル"; fi
 if [ ! -d 1ドル ]; then echo "WARNING: Added 1ドル to $TMP_NAME but 1ドル doens't seem to exist"; fi
 case ":$TMP_PATH:" in # Add trailing :'s to cover first and last entries
 *":1ドル:"*)
 ;; # Exists Already
 *)
 case true in
 2ドル)
 TMP_PATH="1ドル:$TMP_PATH"
 ;;
 *)
 TMP_PATH="$TMP_PATH:1ドル"
 ;;
 esac
 ;;
 esac
 # Remove potential wrapping ":" chars
 TMP_PATH=${TMP_PATH#":"}
 TMP_PATH=${TMP_PATH%":"}
 if [ -z ${3+x} ]; then export PATH="$TMP_PATH"; else export 3ドル="$TMP_PATH"; fi
}

And then, to build my path and remove duplicates I use the following code.

# Set up basic PATH variable
addToPath /usr/local/bin
addToPath /usr/bin
addToPath /usr/sbin
addToPath /bin
addToPath /sbin
# Remove duplicates from path
NEW_PATH=""
IFS=':' read -r -a path_array <<< "$PATH"
for element in "${path_array[@]}"
do
 addToPath $element false NEW_PATH
done
export PATH=$NEW_PATH
janos
113k15 gold badges154 silver badges396 bronze badges
asked Sep 11, 2017 at 16:00
\$\endgroup\$
0

1 Answer 1

3
\$\begingroup\$

Quoting

You should double-quote variables that may contain spaces when used as command arguments. For example here:

addToPath $element false NEW_PATH

You will get strange results if $element contains spaces. Double-quote it, and also in the conditional inside the function:

if [ ! -d "1ドル" ]; then 

One statement per line

It's easiest to read code from top to bottom, without having to carefully read from left to write too. For this reason it would be better to split the conditionals you crammed on a single line.

Simple solutions are better

The optional third parameter of the function is quite clever, appending to a specified variable instead of the default, at the expense of some added complexity. Instead of all that, you could simply reset PATH after setting path_array and before calling the function. That way you could get rid of the third parameter of the function and its added complexity.

Note also that your function does not add duplicates. So instead of removing duplicates at the end, it will be better to remove duplicates at the beginning, as that will process fewer path elements.

case with two cases

When a switch-construct is used with only two cases, it's usually easier to read converted to an if-else.

answered Sep 12, 2017 at 6:52
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.