help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: small problem with BASH_SOURCE and PATH


From: Dennis Williamson
Subject: Re: small problem with BASH_SOURCE and PATH
Date: Fri, 11 Feb 2022 13:40:35 -0600

On Fri, Feb 11, 2022, 12:57 PM Alex fxmbsw7 Ratchev <fxmbsw7@gmail.com>
wrote:

> function absolutebasepath() {
>
> declare arg t=
>
> for arg; do
> if [[ ${t:=$arg} != /* ]]; then
> if [[ $t == */* ]]; then
> r=$PWD/${t%/*}; else
> r=$PWD/.; fi; else
> r=${t%/*}; fi
> absolutebasepath+=( "$r" )
> done
>
> }
>
> i post this in hopes for others
> it returns ( in an array ) its args as aboslute base paths
> useful for 'where script is'
>
> absolutebasepath "$BASH_SOURCE"
> printf 'script is in %s\n' "$absolutebasepath"
>
> ( multiple runs multiple elements )
>
> ${absolutebasepath[0]} is default and if ran multiple then [1] [2] etc
>
> ill hopefully code an assignments only variant and post it, maybe with
> benchmarks
>

http://mywiki.wooledge.org/BashFAQ/028

Also please learn to properly indent your code.

And while many people put then at the end of the if line, you should put
else at the beginning of a line or on a line by itself. And fi should be on
a line by itself.

Putting ${t:=$arg} inside an if is bad practice.

>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]