Skip to content

Instantly share code, notes, and snippets.

@thiagobraga
Last active October 24, 2020 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagobraga/d7adcb15ab7101f99d123b53985e61f3 to your computer and use it in GitHub Desktop.
Save thiagobraga/d7adcb15ab7101f99d123b53985e61f3 to your computer and use it in GitHub Desktop.
Manage PATH environment variable in a more organized way #Linux

Organize PATH environment variable

The idea is to use an array, PATHS for example, to store the folders needed to be in PATH, and use the method join(), taken from this post, to merge correctly all paths.

#!/bin/bash

# Define PATH environment variable in a more organized way.
# ------------------------------------------------------------------------------
PATHS=(
  '/usr/local/sbin'
  '/usr/local/bin'
  '/usr/sbin'
  '/usr/bin'
  '/sbin'
  '/bin'
  '/snap/bin'
  '/opt/sublime_text'
  "$HOME/.config/composer/vendor/bin"
  "$HOME/.gem/ruby/2.5.0/bin"
  "$HOME/.cabal/bin"
  "$HOME/.local/bin"
  "$HOME/n/bin"
)

# Join array elements with delimiter
# @see https://zaiste.net/how_to_join_elements_of_an_array_in_bash/
# ------------------------------------------------------------------------------
join() {
  local IFS="$1"
  shift
  echo "$*"
}

# Make shellcheck happy! :) Export separated from definition.
PATH=$(join : "${PATHS[@]}")
export PATH
@thiagobraga
Copy link
Author

Testing

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment