Michael Queue
Michael Queue

Reputation: 1400

Bash command that prints human readable $PATH outhput not working as an alias in .bash_profile on OS X

This command prints human readable $PATH output.

tr ':' '\n' <<< "$PATH"

However, when I alias the command it does not work. I am sure I need to escape some of the characters, just not sure which ones or how to do it in bash.

alias hpath='tr ':' '/n' <<< "$PATH"'

Upvotes: 3

Views: 104

Answers (2)

anubhava
anubhava

Reputation: 785481

Instead of invoking an external command tr you can do this using BASH's string substitution:

alias paths="echo \"${PATH//:/$'\n'}\""

Or better use a BASH function to avoid esaping:

mypath() {
    echo "${PATH//:/$'\n'}"
}

Upvotes: 5

John Kugelman
John Kugelman

Reputation: 361849

If you use double quotes throughout then you can surround everything with single quotes:

alias hpath='tr ":" "\n" <<< "$PATH"'

Or to have a mix of quote types, surround the alias with double quotes and then escape all the special characters (\", \$, etc.):

alias hpath="tr ':' '\n' <<< \"\$PATH\""

If you're curious, it's possible to use single quotes on the outside, but it's really really hairy. There's no way to escape characters in single quotes, so putting single quotes inside of single quotes requires you to type '\''' ends the string, \' adds a literal quote, and ' starts another string.

alias hpath='tr '\'':'\'' '\''\n'\'' <<< "$PATH"'

Yuck!

Upvotes: 5

Related Questions