1752653 Members
5715 Online
108788 Solutions
New Discussion юеВ

Re: alias command

 
wes glanz
New Member

alias command

I'm trying to create an alias that will list the files in a directory when you
cd to it. I have used the command

alias cd="cd \!* ; ls"

on other systems. But the HP I'm using doesn't seem to recognize the \!* as a
way to imput characters typed in as arguments in the command line. Do I just
have a syntax error or is this not supported. I cann't find it referenced in
any of my documentation.

I'm running 10.20, Korn shell.
2 REPLIES 2
Anthony Goonetilleke_1
Regular Advisor

Re: alias command

You may find that you used

alias cd "cd \!* ; ls"

on csh/tcsh or a variant if you are using Ksh try something like the following

alias cd="cd $1; ls"

as "cd" only gets one ARG passed to it you should be safe with the $1.
Dan Hull
Regular Advisor

Re: alias command

Anthony's suggestion is a good one at first glance, but unfortunately this
doesn't work :) The $1 variable is not set when the alias command is executed,
so you end up with cd being aliased to "cd ; ls". This will cause any cd
command to jump to your home directory and then do an ls. The Korn shell
doesn't give you any warning that this won't work, but the Posix shell does by
reporting "sh: 1: Parameter not set."

The only way I know to do what is wanted here is to use a function. Just add
the following to your .kshrc or other shell startup script. Keep in mind that
if you want it to happen in your CDE terminal windows, you'll need to set CDE
up to source your regular profiles, or ad these lines to your .dtprofile:

function cdl
{
cd $1
ls
}
alias cd=cdl

This will create the cdl function, which will do whatever is inside the braces.
Make sure you put the alias command after the function. If you put it first,
it runs into a loop of some sort and doesn't work (because cd = cdl before the
function uses it I guess).

The only problem with this simple of a function is that if you give it an
invalid directory, it will give an error and then list the current directory,
often scrolling the error off the screen. You may want to add some error
checking to your function.