我想为ls -rt创建一个快捷函数,但是在我的定义中,-rt是不被Unix接受的。为什么这不起作用?解决办法是什么?
lsr () {ls -rt $1;}发布于 2016-06-14 03:07:14
您的特殊问题是,在开头大括号之后需要空白,例如(适当地引用并使用所有参数)。
lsr() { ls -rt "$@"; }但是,您也可能需要考虑别名,因为这是命令开始时的替换:
alias lsr='ls -rt'毕竟,有多少系统为您提供了非常方便的“长ls",包括:
alias ll='ls -al'您只需要确保在需要的特定情况下(例如在.bashrc中用于交互式shell),或者在脚本本身开始时提供的文件中(并确保别名使用shopt -s expand_aliases进行扩展)就会出现这种情况。这是两种可能性,但毫无疑问还有其他的可能性。
发布于 2016-06-14 03:00:21
在{之后添加空格
lsr () { ls -rt $1;}
发布于 2016-06-14 03:05:20
会不会是第一个"{“之后的空白?
# bash --version
GNU bash, version 4.3.42(1)-release (i686-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
#
# touch a
# touch b
# touch c
# ls -rt
a b c
# ls -t
c b a
#
# lsr() {ls -rt;}
bash: syntax error near unexpected token `{ls'
# lsr() { ls -rt;}
# lsr
a b chttps://stackoverflow.com/questions/37802194
复制相似问题