making vars in bash script accessible from shell

Michael O'Donnell mod+gnhlug at std.com
Thu Nov 7 21:13:30 EST 2002


>I've tried "export newPWD", which doesn't do anything either.
>
>This is obviously some fundamental rule of variable scope in
>bash that I just don't know, so can someone set me right?


If your goal is to be able invoke that code from your normal
interactive BASH session then I think the solution is to
munge your code along the following lines and then add it to
your .bashrc:


pwdmaxlen=30            # How many characters of the $PWD should be kept.
trunc_symbol="..."  # indicator that there has been directory truncation.

function clippedPWD()  {              # ...or whatever you wanna call it.
   if [ ${#PWD} -gt $pwdmaxlen ]
   then
       local pwdoffset

       pwdoffset=$(( ${#PWD} - $pwdmaxlen ))
       echo "${trunc_symbol}${PWD:${pwdoffset}:${pwdmaxlen}}"
   else
       echo "${PWD}"
   fi
}



...after which you'll be able to invoke it interactively thus:

    newPWD=`clippedPWD`


 .




More information about the gnhlug-discuss mailing list