[clug] Bash questions...

David Deaves David.Deaves at dd.id.au
Mon May 2 00:10:26 MDT 2011


> 
> Anyone done 'substrings' in bash?
> 

sep=$(for i in {1..200};do echo -n '-';done)

echo "${sep:1:20}"


There are some very cool things you can do with strings in modern bash, you 
almost never need to fork to some other more capable program (sed, expr, awk ...)

of interest the old standards basename and dirname are easily re-implemented 
with the  "${path##*/}" (the single arg usage)  and  "${path%/*}".
To do the more complete 2 arg basename you would need:

bname()
{
  file="${1##*/}"
  echo "${file%$2}"
}

all fork free.

Dave !




More information about the linux mailing list