LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 10-02-2009, 07:06 AM   #1
arifd86
Member
 
Registered: Aug 2009
Distribution: Ubuntu 19.04
Posts: 30

Rep: Reputation: 5
my custom cd command


hello again.

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!

Last edited by arifd86; 10-02-2009 at 07:08 AM.
 
Old 10-02-2009, 08:19 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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".
 
Old 10-02-2009, 09:03 AM   #3
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
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.
 
Old 10-02-2009, 02:42 PM   #4
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
I agree with all the above but will just note that you could also keep the function as a function and keep its name with the 'command' builtin:
Code:
function cd() {
    command cd $1 && ls
}
 
Old 10-02-2009, 03:19 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Good point slakmagik. Thanks for the info.

Always something new to learn when it comes to bash, isn't there?
 
Old 10-02-2009, 03:41 PM   #6
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
Always something new to learn when it comes to bash, isn't there?
Absolutely.

While Chet Ramey is speaking from the dev point of view, I like his sig:
Quote:
``The lyf so short, the craft so long to lerne.'' - Chaucer
 
Old 10-02-2009, 05:06 PM   #7
Chromezero
Member
 
Registered: Nov 2004
Location: Arizona
Distribution: Slackware, RHEL, others
Posts: 470

Rep: Reputation: 40
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'
 
Old 10-02-2009, 05:38 PM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by Chromezero View Post
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.
 
Old 10-05-2009, 11:12 AM   #9
Chromezero
Member
 
Registered: Nov 2004
Location: Arizona
Distribution: Slackware, RHEL, others
Posts: 470

Rep: Reputation: 40
I've been using this alias for years and it seems to work fine for me. To be more specific, it looks like this in my .bashrc...
Code:
set prompt = "[`hostname`]{`uname`}-`pwd`-> "
  alias cd 'cd \!* >/dev/null ; set prompt = "[`hostname`]{`uname`}-`pwd`-> " ; ls'
This sets my prompt to a custom format. Then, the alias for cd will update my prompt with my "pwd" and do an ls.

Last edited by Chromezero; 10-05-2009 at 11:18 AM.
 
Old 10-05-2009, 11:30 AM   #10
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
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.
 
Old 10-05-2009, 11:38 AM   #11
Chromezero
Member
 
Registered: Nov 2004
Location: Arizona
Distribution: Slackware, RHEL, others
Posts: 470

Rep: Reputation: 40
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.
 
Old 10-05-2009, 12:05 PM   #12
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
This would work in bash:

Code:
alias cd='cd;ls'
But this won't:

Code:
alias cd 'cd;ls'
And your other example above, won't either:

Code:
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.
 
Old 10-05-2009, 12:46 PM   #13
Chromezero
Member
 
Registered: Nov 2004
Location: Arizona
Distribution: Slackware, RHEL, others
Posts: 470

Rep: Reputation: 40
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.
 
Old 10-05-2009, 03:04 PM   #14
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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?
 
Old 10-05-2009, 07:54 PM   #15
Chromezero
Member
 
Registered: Nov 2004
Location: Arizona
Distribution: Slackware, RHEL, others
Posts: 470

Rep: Reputation: 40
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Command Prompt ArcaneChaos Linux - General 1 07-24-2008 09:37 AM
webmin custom command Ammad Linux - General 1 07-22-2007 09:19 AM
Custom command Maverick1182 Linux - Newbie 1 03-06-2007 08:55 AM
Setting a custom command drimades Linux - Kernel 1 11-15-2006 09:04 AM
custom cd command glingon Linux - General 4 11-01-2004 07:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration