Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have found myself keep doing cd some_dir quickly followed by ls for quite some time now, and have been trying to write a bash alias to let me do this, such as:

alas cd="cd $@; ls";

Problem with this is you can't have an input argument in the middle of an alias (correct me if I'm wrong).

I've also tried defining my own shell function as suggested by this page:

cd() { cd "$@"; ls; }

But the problem with this one is if I try to use it like so:

cd Documents

the shell exits with [Process completed], rendering the shell useless... So how should I get around this?

share|improve this question

migrated from stackoverflow.com Nov 19 '12 at 14:05

This question came from our site for professional and enthusiast programmers.

1  
I'd love a shell that had alas as a builtin. –  Rob Davis Nov 17 '12 at 19:45

3 Answers 3

up vote 10 down vote accepted

Try this:

cd() { builtin cd "$@" && ls; }

builtin makes the cd inside the function invoke the builtin cd command rather than trying to call the function recursively.

share|improve this answer
    
Is recursion actually a risk? I do alias ls='ls --color', and it doesn't recurse. Nor does 'alias rm='rm -i' –  David Yaw Nov 17 '12 at 13:53
3  
Those are aliases, not a functions. They don't work the same way. Try foo() { echo hello ; foo; }; foo; (have your Ctrl-C ready). –  Mat Nov 17 '12 at 13:54
    
Ah, quite right, wasn't paying enough attention. –  David Yaw Nov 17 '12 at 13:58
3  
Might be better to have builtin cd "$@" && ls; instead so that ls doesn't run if there is problem entering the directory (non-existent etc.), like what John Ledbetter mentioned in his answer. –  doubleDown Nov 17 '12 at 14:13
    
Yep, that's much better; hadn't thought about that, thanks. –  Mat Nov 17 '12 at 14:17
     alias cd='cd $@ &&  ls $@ --color '

cd any directory name it can be display the all content.The alias set to cd command in the .bashrc file and run the . .bashrc file. it's change in to all place

share|improve this answer

In GNU Bash,

PROMPT_COMMAND=ls
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.