Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a desire that every time i type in 'cd /whatever' it automatically performs an 'ls' right after. So i put this into my .bashrc, with no luck:
Code:
# do 'ls' after every 'cd'
function cd() {
cd $1
ls
}
but this just causes the cd function to hang, even when i supply an argument such as 'cd Desktop'. Also, I really want it to whack on the * wildcard to every cd argument, unless i add an option like -x to not wildcard, but i wouldn't know where to begin coding that, any help? super-thanks!
A little testing shows that you're getting caught in an infinite regression loop, because your function has the same name as the command inside it. The function is trying to call itself. If you change the function name to something different it works as expected.
It appears that you can then alias cd to your function name and it will work the way you want it to.
I would also change the command to 'cd $1 && ls', so that ls only runs if the cd is successful.
And I have no idea what you mean by "whack on the * wildcard".
The solution to the problem is above, so I'll spare that.
I have one comment though: I'd rather use "cd $@ && ls". It's not a common case, but cd *can* have more than one argument Also, as said above, && will avoid useless ls output if cd fails.
Why not just use an alias in your .bashrc or source a .alias file? Like this...
Code:
alias cd 'cd ; ls'
I have the same thing in my .alias file since I'm lazy. Along with others like...
Code:
alias lsa 'ls -a'
alias ls 'ls --color'
alias lsl 'ls -l'
alias lsc 'ls --color'
alias lslc 'ls -l --color'
alias lsac 'ls -a --color'
alias ping 'ping -c 4'
Why not just use an alias in your .bashrc or source a .alias file? Like this...
Code:
alias cd 'cd ; ls'
That won't work for many reasons:
at least in bash, that syntax is incorrect. The correct way includes a '=' sign between the left and the right, like in alias cd='cd;ls'
even then, if you do that, "cd /tmp" will be expanded to "cd; ls /tmp". The effect of that command is to run first "cd" (without any argument), which will take you to your home directory, then run "ls /tmp", which will show the contents of /tmp".
even if you use $1, that won't change anything, because aliases don't do positional parameters expansion. Functions do, and that's why we are using them when we need to pass arguments.
Are you sure you really mean "bash"? Maybe it's some csh variant what you use. That syntax is simply incompatible with bash, unless you are using a patched version of some kind. Maybe your .bashrc is symlinked to .tcsh or some other funny thing, maybe you are not using the shell you think you are using.
In bash, $prompt has no meaning. You have to set $PS1 to change your prompt.
That's correct, with BASH you may need to use "set $PS1". However, the alias of cd to "cd ; ls" still works just fine. I use tcsh at work and bash at home and have the same basic alias in both locations.
Last edited by Chromezero; 10-05-2009 at 11:41 AM.
alias cd 'cd \!* >/dev/null ; set prompt = "[`hostname`]{`uname`}-`pwd`-> " ; ls'
Because:
there's no equal sign
!* is nothing in bash, I guess that it's the csh equivalent for $@ or maybe $*
$prompt vs $PS1
So, it's impossible that you use the same exact aliases in both shells. Most aliases could work if they are simple enough and don't include any csh specific code on them. But this is not the case here. And since the OP is talking about a .bashrc file, we should really concentrate on code that's correct for sh/bash, and not csh, ksh or any other shell. Not that I have anything against them, but it's really not the topic.
My original intent was merely to suggest another approach. The syntax may vary from one shell to another, but it can certainly be done with an alias, regardless of your chosen shell. This is the reason I said "same basic alias", meaning it's similar but not identical in both shells. When possible, I'll post my alias from home system on which I use BASH.
Aliases are just simple string substitutions. The text on the left side is simply replaced with the text on the right side before execution. As explained earlier, and as the bash man page clearly states, there's NO provision for inserting arguments from the command line into the middle of the string.
So no, a simple alias like alias cd='cd;ls' will NOT work properly in bash since there's no way to pass the directory name to the cd command. It is a legal command, but it won't do what you want it to, except to go $HOME. And I tested it myself before posting just to be sure.
Obviously the command posted earlier uses the ability of the prompt variable to run embedded commands as a workaround; using the alias to reset the prompt on the fly, which in turn runs the commands as part of its operation. But while I don't doubt it works in tcsh, it still appears to fall victim to the above limitation in bash. If \!*, or anything like it, exists in bash as a way to pass the argument to cd, I haven't been able to find it in the documentation. A few quick tests of the above certainly didn't do me much good. I'd certainly like to see working code if you have it.
Even if it does work, though, using the alias to call the prompt to run the commands is not much different than using an alias to call a function or external script to run the commands. So why bother when a simple shell function works just as well, is easier to understand, and is the recommended way to go about it anyway?
Thanks to David the H and i92guboj for pointing out my error. I was sure that an alias could be used in BASH as well as TCSH, and it turns out I was wrong. I do have this setup in my .bashrc but as a function call rather then an alias. The alias is still valid for TCSH, just not for BASH. My appologies.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.