f



How to alias "n1" to "n 1" and "n2" to "n 2" and so on (with style)

I have the following bash function:

function ..()
{
    counter=0

    case "$1" in
	[0-9][0-9] )
	    while [ $counter -lt $1 ]
	      do
	      cd ..
	      counter=$(($counter+1))
	    done
	    ;;
	
	* )
	    echo "staying where I am, give me a number (<99) next time :)"
	    ;;
    esac

}

The allows me to move up, say 4 levels, using ".. 4"

Now, typing the space annoys me so I define the following aliases:

alias ..='.. 1'
alias ..2='.. 2'
alias ..3='.. 3'
alias ..4='.. 4'
alias ..5='.. 5'

Not very elegant I concede, so I'd like a nice way to set "..n" to
mean ".. n" (where n is a number obviously :) )

Ideas?
0
bbq_pit
8/28/2003 5:03:07 PM
comp.unix.shell 15484 articles. 3 followers. Post Follow

1 Replies
810 Views

Similar Articles

[PageSpeed] 6

laura fairhead <laura_fairhead@btinternet.com> wrote:
> On 28 Aug 2003 10:03:07 -0700, bbq_pit@hotmail.com (BBQ) wrote:

>>Now, typing the space annoys me so I define the following aliases:
>>
>>alias ..='.. 1'
>>alias ..2='.. 2'
>>alias ..3='.. 3'
>>alias ..4='.. 4'
>>alias ..5='.. 5'
>>
>>Not very elegant I concede, so I'd like a nice way to set "..n" to
>>mean ".. n" (where n is a number obviously :) )
>>

> I would just type it out, in UNIX you are always going to
> have to be prepared to type... I suppose you could
> make it shorter, getting rid of the function code, by just
> aliasing the commands;

> alias ..='cd ..'
> alias ..2='cd ../..'
> alias ..3='cd ../../..'
> alias ..4='cd ../../../..'
> alias ..5='cd ../../../../..'
> alias ..6='cd ../../../../../..'

You could even automize this:

#########################################

function ..
{
    for (( n = $1; n > 0; --n ))
    do
        cd ..
    done
}

for (( d = 1; d < 20; ++d ))
do
    eval alias ..$d="'.. $d'"
done

#########################################

I don't think this makes too much sense, though...

Regards,
Bernd

-- 
Bernd Eggink
Regionales Rechenzentrum der Uni Hamburg
Bernd.Eggink@rrz.uni-hamburg.de
http://www.rrz.uni-hamburg.de/RRZ/B.Eggink/
0
Bernd
8/28/2003 9:29:21 PM
Reply: