Glam Prestige Journal

Bright entertainment trends with youth appeal.

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?

4

4 Answers

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.

8

In GNU Bash,

PROMPT_COMMAND=ls

I have come up with a Bash script that should work to make this happen.

# Automatically do an ls after each cd
cd() { if [ -n "$1" ]; then builtin cd "$@" && ls --group-directories-first else builtin cd ~ && ls --group-directories-first fi
}
 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

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy