1

I looking for a way to easier manage my $PATH variables.

executing $PATH returns me:

/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin: No such file or directory

To me this is total crap and hard to read.

Can they be listed like so:

/usr/local/bin:
/usr/local/sbin:
/usr/local/mysql/bin:
/opt/local/bin:
/opt/local/sbin:
/usr/bin:/bin:
/usr/sbin:
/sbin:
/usr/local/bin:
/usr/X11/bin: No such file or directory

I'd love to manage my $PATH using Ruby but naturally it looks like I needs to stay as part of my shell configuration files.

3
  • If you are just looking to display it nicer you could use a command like this. echo $PATH | sed -e 's/:/\n/g'
    – Zoredache
    Feb 8, 2010 at 17:58
  • Can I put them into my profile on separate lines? Feb 8, 2010 at 18:32
  • Another display incantation: printenv PATH | tr ":" "\n". For manipulating it in scripts see (on Stack Overflow) stackoverflow.com/questions/273909/… Feb 9, 2010 at 23:57

5 Answers 5

1

You can list your pathspec out on multiple lines like so:

PATH=\
/a:\
/b:\
/c:\
/d

You may also append stuff to your path by doing

PATH=${PATH}:/new:/stuff

so you could have

PATH=/a
PATH=${PATH}:/b
(etc)

At the risk of sounding like a crotchety old *NIX admin, please don't do strange and funky things like writing a ruby script to manage PATH for you -- You aren't going to be editing this very often (if you are you're probably doing something wrong), and you will be better served in this case by conforming to the same standards as everyone else, especially if/when you need to ask someone for help.

The colon-delimited path list isn't so terrible in the grand scheme of badly formatted things you'll find in the world :-)

1
echo $PATH | sed 's/:/:\n'

You can do something along the lines of:

PATH=$(ruby_prog $PATH)

in a startup script (or other script that is sourced) or at the shell prompt.

1

$PATH needs to look the way it does, though you can probably split it across lines using a backslash (untested, don't have a *nix or Linux machine available).

Why don't you write a Ruby script to parse it for you, and make it readable, and allow you to edit it? That'll be a nice little project.

1

If your server has Perl, and you don't mind using it, then you might try this one-liner to display your PATH (not manage it):

printenv PATH | perl -nl -0x3A -e 'print ++$i, " ", $_;'

I wrote the details about this on my tech BLOG.

0

On most servers we have both 64-bit and 32-bit application environments.

Everything 32-bit is compiled into /opt/32, everything 64-bit into /opt/64.

We then have a "set_env" script in both folders which sets the relevant PATH and LD_LIBRARY_PATH variables. If we want to run something 32-bit, we type

. /opt/32/set_env

or 64-bit:

. /opt/64/set_env

The format is, as voretaq7 suggested, laid out like so:

export PATH="\
/opt/32/app1/bin:\
/opt/32/app2/bin:\
/opt/32/app3/bin"

(replace 32 with 64 in the 64-bit script.)

Just don't do what I did originally and indent the lines, because then it becomes " /opt/32..." - note the spaces, which then isn't a valid filesystem path!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .