views:

44

answers:

3

I want an alias that produce folowing result:

$cd /home/ok

[clear screen]
/home/ok
total 452K
-rwx--x--x 1 user gigl  16K Oct  1 14:08 ok0
drwx------ 5 user gigl    0 Oct  1 14:02 ok1
drwx------ 5 user gigl    0 Oct  1 13:59 ok2
drwx------ 9 user gigl    0 Oct  1 14:01 ok3
-rw------- 1 user gigl   32 Sep 30 14:36 ok4

I did a script like

$cat ~/.cd.sh
#!/bin/bash
cd $1 && clear && pwd && ls -lh --color=auto

But it does not change the current dir. This is probably because in the script it will change the dir but when it goes back to bash I'm back in the dir I executed the script.

Any idea ?

Thanks, from answers I got something like that working great:

alias ls="clear && pwd && ls -lh --color=auto"
cd() { builtin cd "$1" && ls; }
+5  A: 

Personally, I'd recommend writing a function rather than an alias loaded from your .bashrc or .bash_profile.

The function would look pretty much like what you've already got:

cdd () {
  cd "$1"
  clear
  pwd
  ls -lh --color=auto
}

I'm not positive why the alias causes you to go back, but I tested the function and it works.

Bryan
I think the question wasn't clear. His script went back, and he can't figure out how to get an argument into an alias. As you say, a function is the proper solution.
Darron
For standards compliance, avoid the `function` keyword. Use a syntax like `cdd(){ }`. If you do that, and replace `$1` by `"$1` (for directories with whitespace), you'll get a +1 from me.
Lekensteyn
@Lekensteyn: Good points. Fixed.
Bryan
Great worked, I wrote this function in my .bash_profile, but I cant alias so it become only cd because it give me an infinite loop :-(
Guillaume Massé
@Guillaume: Change the line from `cd "$1"` to `builtin cd "$1"` to prevent the infinite loop. You also don't need to alias it, just call the function `cd`.
Dennis Williamson
@Dennis thanks man see my edit above
Guillaume Massé
A: 

Use a function instead, for example

mycd() { cd "${1?}" && clear && pwd && ls -lh --color=auto; }
jilles
Is the question mark a typo?
Dennis Williamson
The question mark causes an error if no parameter is passed. It is not as pretty as a manual check but yields shorter code :)
jilles
builtin cd does not generate error with no params. this could be usefull not to clear the screen. The ? normally mean optional, it's crazy they throw an error for that. I would except not having an error at all.
Guillaume Massé
I suppose `cd "$@"` would be more useful than the other proposals.
jilles
A: 

I don't think you want && either. This will run each command simultaneously, iirc. You should use semicolons.

0x90
No, it doesn't run them simultaneously - it runs each one conditionally on the success of the preceding one. A single ampersand would run them in the background.
Dennis Williamson
You're correct. Semicolons would run them regardless of the previous exit status.
0x90