You are not logged in.

#1 2009-11-12 19:23:00

murfMan
Member
Registered: 2009-10-27
Posts: 161

add directory to PATH

i have a directory ~/scripts

does anyone know  how to add this to my PATH so that i can use tab completion?

Offline

#2 2009-11-12 19:38:24

lldmer
Member
From: Amsterdam
Registered: 2008-05-17
Posts: 119

Re: add directory to PATH

http://tldp.org/LDP/Bash-Beginners-Guid … 03_02.html

$PATH is a global variable. You can assign a global variable like

export variablename="value"

Without the 'export' it would be a local variable, visible only in the current shell instance. Because you want to pass this variable to programs or scripts you run, you make it global.

To make this permanent/default put the statement in your ~/.bashrc (or your shells config if you're using something else than bash), so it will execute as soon as you start your shell. For example a line from my config:

[[ -d $HOME/.bin ]] && export PATH=$HOME/.bin:$PATH

As you can see with `echo $PATH`, the paths in $PATH are separated by a ':'.  So I prepend the value of $HOME (your homefolder) and a ':' to the current value of $PATH, and assign it to $PATH.


For lack of better words: chair, never, toothbrush, really. Ohw, and fish!

Offline

#3 2009-11-12 20:28:28

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: add directory to PATH

i read this somewhere as the "right" way to do it.  using this approach only adds the new directory/ies if they aren't already present so you don't eff up your PATH if you re-source .bashrc again later.

# what to add to path
NPATH="$HOME/.bin"

# add it only if required
case ":${PATH}:" in
  *:${NPATH}:*) ;;
  *) PATH=${PATH}:$NPATH ;;
esac

export PATH

you could easily add the check if the directory exists first.  you could also reorder PATH and NPATH depending on which should take precedence.

Offline

#4 2009-11-13 06:12:56

perbh
Member
From: Republic of Texas
Registered: 2005-03-04
Posts: 765

Re: add directory to PATH

@brisbin33
Yup - that is a very wise suggestion ...
Personally I have something I call 'brush_path' where I can prepend/append directories to the original $PATH (directories with a leading '-' are prepended and a '+' appends them - it goes something like this:

#!/bin/sh
# process arguments - only those starting with '-' or '+' are honoured ...
preps=""
apps=""
for a in $*; do
  case $a in
    -*) preps="$preps ${a#-}" ;;
    +*) apps="$apps ${a#+}" ;;
  esac
done
# get the complete path
dirs="`echo $preps $PATH $apps | tr ':' ' '`"
# clean it all up ...
newpath=""
for d in $dirs; do
  ok=yes
  for p in $newpath; do
    test "$d" = "$p" && { ok=no; break; }
  done
  test "$ok" = yes && newpath="$newpath $d"
done
echo $newpath | tr ' ' ':'

Now - as one of the last things I do in .bashrc is:

test -f /usr/local/bin/brush_path && export PATH="`sh /usr/local/bin/brush_path -~/bin -/usr/local/bin"

Last edited by perbh (2009-11-13 06:18:29)

Offline

#5 2009-11-13 06:34:44

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,365
Website

Re: add directory to PATH

Looks complicated....

> head -1 .bashrc
PATH=~/bin:$PATH

No need for checking given you know that your scripts directory is present...

Offline

#6 2009-11-13 16:35:51

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: add directory to PATH

In my config.fish:

# fish does not allow setting nonexistant dir to PATH
# Arch default includes nonexistant directories
# ~ echo $PATH
# /bin /usr/bin /sbin /usr/sbin /usr/bin/perlbin/site /usr/bin/perlbin/vendor /usr/bin/perlbin/core
# We'll set the PATH manually for now.
set -l myPATH $PATH
for i in $myPATH
        if test -d $i
                if not contains $i $newPATH
                        set newPATH $newPATH $i
                end
        end
end
set PATH $newPATH
set -e newPATH

Otherwise I end up with no PATH at all when I try to add anything.


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#7 2009-11-13 20:35:23

lldmer
Member
From: Amsterdam
Registered: 2008-05-17
Posts: 119

Re: add directory to PATH

Allan wrote:

No need for checking given you know that your scripts directory is present...

True... But I share this config between multiple computers:P


For lack of better words: chair, never, toothbrush, really. Ohw, and fish!

Offline

Board footer

Powered by FluxBB