Pass wildcard to alias

那年仲夏 提交于 2019-12-10 14:46:04

问题


I use a modifies list command as alias (in KSH):

alias ltf='ls -lrt -d -1 $PWD/*'

So the command ltf displays something like this:

-rw-r--r-- 1 myuser mygroup 0 Apr 18 12:00 /usr/test.txt
-rw-r--r-- 1 myuser mygroup 0 Apr 18 12:00 /usr/test.log

Now I want to use wildcards. But using ltf *.log does not work.

What is the best way to achieve that?


Update: I want to specify my question because the answers does not solve my problem so far: the command ls -lrt -d -1 $PWD/* executes a list command with some options AND it displays the full path, which is an important point for me.

Unfortunately the alias approach does not allow wildcard parameters. My goal is to make this possible. Probably it is the best way to create the command as a function. This is mentioned in the answers, but it does not work yet (see comments).

Any ideas?


回答1:


Use a shell function instead of an alias:

function ltf {
  if [ -z "$1" ]; then
    ls -lrtd1 ${PWD}/*
  else
    ls -lrtd1 $1
  fi
}



回答2:


try

alias ltf='ls -lrt -d -1 $1'

or if you want many params

alias ltf='ls -lrt -d -1 $@'



回答3:


Your problem is that the wildcards are getting expanded by the shell in your current directory, not in $PWD. You can solve this by using a shell function rather than an alias and doing some quoting (I use $HOME in the example for my convenience) - put this in .bashrc:

ltf() {
    ls $HOME/$*
}

and then:

$ ltf '*.log'


来源:https://stackoverflow.com/questions/5699749/pass-wildcard-to-alias

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!