开发者

help with using alias in bash shell

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?


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.


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

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


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 Why doesn't "cd" work in a bash shell script?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜