Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 6, 2020 10:54 am GMT

Dotfiles - Shell Config

Content for Bash config ~/.bashrc or ZSH config ~/.zshrc

See my full Shell config file on GitHub.

OS flags

For any values in configs or aliases which are OS-specific, I found it useful to determine the OS with logic once-off and then reuse the flags.

IS_MAC=falseIS_LINUX=falsecase "$(uname -s)" in  Darwin*)    IS_MAC=true    ;;  Linux*)    IS_LINUX=true    ;;esac

That lets me then use the variable to perform OS-specific behavior. Here I add a Brew-installed Ruby gems path to the PATH value, just for macOS.

if [[ "$IS_MAC" == 'true' ]]; then  export PATH="/usr/local/opt/ruby/bin:$PATH"fi

Setting PATH the smart way

Part of the standard setup of packages like Ruby, Go and Node is to add a directory to your shell PATH variable so that executables can be found.

If I want to check the directory exists first, I could do this:

if [ -d '/usr/local/opt/ruby/bin' ];  export PATH="/usr/local/opt/ruby/bin:$PATH"fi

But I got tired of having that long syntax. So I made a function to do that, which I call like this:

prepend_path() {  if [ -d "$1" ]; then    export PATH="$1:$PATH"  fi}

Usage:

# Ruby gemsprepend_path /usr/local/opt/ruby/bin# Go packages.# Traditional path is '/usr/local/go/bin' or '/usr/bin'.# I created ~/.local to use that rather.prepend_path ~/.local/go/bin

Executing local packages

When working on Node projects, you normally have to run a script like this in the shell:

$ ./node_modules/.bin/eslint

I'd rather just run this:

$ eslint

To get that behavior, I decided to add ./node_modules/.bin to my PATH variable.

prepend_path_always() {  export PATH="$1:$PATH"}prepend_path_always './node_modules/.bin'

Note that the values in PATH are read from left to right, so you must make sure your local packages are read before any global packages. In case you have say eslint installed locally and globally.

View the PATH

Here is what my PATH value looks like.

$ split_path/home/michael/.local/go/bin/home/michael/.deno/bin./node_modules/.bin/home/michael/.local/bin...

Note that the PATH value normally looks like this with colon separators, making it hard to read.

/home/michael/.local/go/bin:/home/michael/.deno/bin:...

So I created an alias called split_path which makes that more human-readable.

More about aliases in the next post in this series...


Original Link: https://dev.to/michaelcurrin/dotfiles-shell-config-5dnf

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To