cd then ls -l


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cd then ls -l
# 1  
cd then ls -l

Hi,

I'm trying to create a little script:

if [ -d $1 ]
then
cd $1
ls -l $1
fi

so I can make a new command: cl that does both.

simple enough but I noticed that after executing I'm returned to my original location. How do I surpress that behaviour? I'm using bash-2 on HP-UX 11.

Thanks.
# 2  
That's because it's executed in a subshell.

You could add a function to .bashrc something like

Code:
cl()
{
   if [ -d "$1" ]; then
      cd "$1"
      ls -l
   fi
}

Cheers
ZB
# 3  
Have a question here ....
See the following 2 aliases ....

alias cl="cd scripts ; ls -lrt"
alias cla="cd $1 ; ls -lrt"

Here is the concern ...
1)After running the first alias command 'cl' , the current directory is $HOME/scripts
2) After running the second alias command 'cla scripts' , the current directory is $HOME only.

For both alias commands , o/p is nothing but list of "scripts" directory.

Any ideas what's happening here .... ?
# 4  
Quote:
Originally Posted by bhargav
alias cla="cd $1 ; ls -lrt"
That is the ksh syntax for an alias. But an alias in ksh cannot have parameters. That $1 will not be the parameter on the alias invocation. Use a function if you want parameters.
# 5  
. ./yourscript.sh
# 6  
the result, by the way

a bit late maybe but I thought I might post the result:

cl()
{
if [ -d $1 ]
then
cd $1
ls -la
else
cd $HOME
ls -la
fi
}

then

./cl.sh

in my .profile works very well on ksh and in bash
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #785
Difficulty: Medium
The TRS-80 Model 100 was a desktop-style computer introduced in 1983.
True or False?

Featured Tech Videos