tags:

views:

72

answers:

4

i want to have an alias "t" to enter a folder and list the content there.

i tried with:

alias t="cd $1; ls -la"

but it just listed the folder i typed but did not enter it. i wonder why?

cause when i use this one:

alias b="cd ..; ls"

it went back to the parent and listed the content.

so i want the "t" do enter the folder i type in too.

someone knows how to do this right?

+4  A: 

You can't pass arguments into bash aliases. You'll need to create a shell function like so:

function t { cd "$1" && ls -la; }

Edit: whoops, forgot the function and edited per Juliano's suggestion.

jimyi
`bash: syntax error near unexpected token '}'`
Thomas
`function t` or `t()` at the beginning.
Ignacio Vazquez-Abrams
Juliano
+2  A: 

I don't think you can use aliases that way. You can, however, declare a function:

function t {
    cd "$1"
    ls -la
}
Thomas
+1  A: 

cd is tricky in bash. The command you issued ran in a separate process than your bash shell, and that process terminated when it was done. See http://stackoverflow.com/questions/255414/why-doesnt-cd-work-in-a-bash-shell-script

Michael Munsey
Nope, that's not the reason in this case.
Dennis Williamson
The `cd` command (chdir) cannot be implemented as an external. It must be a shell built-in in order to be useful. (That's because it changes the shell's own process state). For similar reasons the `read` and `export` and `set` commands must also built-ins.
Jim Dennis
+1  A: 

In most UNIX shells (csh, bash, zsh) aliases are a form of expansion. Thus they are not parsed like functions. Any word in the interactive input stream which would be processed as a command will be scanned against the list of aliases and a simple string replacement will be performed (usually before any other forms of expansion).

If you need to process arguments then you want to define a function which is parsed and processed rather than simply being expanded like a macro.

Jim Dennis