0

I have a folder with materials for university study, sorted by semesters:

$ ls University
semester1 semester2 semester3 semester4

I'm trying to make one of them the named directory, and I want zsh to allways pointed to directory ending with highest number (so I don't have to update my directory shortcut every semester).

So far I found only the zsh expansion <->:

$ ls semester<->
semester1 semester2 semester3 semester4

but I cannot find a way to extract only the last dirname from that.

Any idea how I should proceed or what I should change?

4

1 回答 1

1
latestSemester=`ls semester<-> | tail -1`
echo $latestSemester

实际上这也有效

latestSemester=`ls semester<->([-1])`

编辑:修复了第二行,其第一个版本缺少括号。

来自 zsh 手册

[beg[,end]]
    specifies which of the matched filenames should be included in the returned list. The
    syntax is the same as for array subscripts. beg and the optional end may be mathemat-
    ical expressions. As in parameter subscripting they may  be  negative  to  make  them
    count  from the last match backward. E.g.: ‘*(-OL[1,3])’ gives a list of the names of
    the three largest files.
于 2013-04-18T09:24:28.450 回答