将目录分配给源文件中的变量

时间:2022-04-03 23:15:53

I am building a source file with some alias to executable files (these are working just fine) and assigning directories to variables in order to get to the directory quicker, with less typing. For example, if I source example.source:

我正在构建一个带有可执行文件别名的源文件(这些工作正常)并将目录分配给变量,以便更快地到达目录,减少输入。例如,如果我源example.source:

#!/usr/bin/bash
mydir="/path/to/some/dir"

I can get to /path/to/some/dir with

我可以使用/ path / to / some / dir

cd $mydir

However, I am not being able to use tab complete to navigate through other sub-directories like I would do by typing the complete path. I mean, if I use the tab key to complete the variable I get cd $mydir but not cd $mydir/ (I have to delete the last space character and manually type the slash / to see the next sub-directories). Hope this is an understandable question. Is there any workaround for this?

但是,我无法使用tab complete来浏览其他子目录,就像输入完整路径一样。我的意思是,如果我使用tab键来完成变量我得到cd $ mydir而不是cd $ mydir /(我必须删除最后一个空格字符并手动键入斜杠/以查看下一个子目录)。希望这是一个可以理解的问题。这有什么解决方法吗?

EDIT: the linux distribution I'm using is Slackware Linux 3.2.31.c x86_64 GenuineIntel GNU/Linux

编辑:我正在使用的Linux发行版是Slackware Linux 3.2.31.c x86_64 GenuineIntel GNU / Linux

EDIT2: GNU bash, version 4.2.37(2)-release

EDIT2:GNU bash,版本4.2.37(2) - 发布

2 个解决方案

#1


1  

Apparently this feature is starting to be implemented in bash 4.3, release 26-Feb-2014 09:25.

显然这个功能开始在bash 4.3中实现,发布时间为2014年2月26日09:25。

Reading the NEWS file in bash 4.3 I found this:

在bash 4.3中阅读NEWS文件我发现了这个:

i. The word completion code checks whether or not a filename containing a shell variable expands to a directory name and appends `/' to the word as appropriate. The same code expands shell variables in command names when performing command completion.

一世。单词完成代码检查包含shell变量的文件名是否扩展为目录名,并在适当的位置附加“/”。执行命令完成时,相同的代码会扩展命令名中的shell变量。

Unfortunately I cannot do a de novo installation of bash (because I'm working on a server) but I hope this can help others.

不幸的是我无法重新安装bash(因为我在服务器上工作),但我希望这可以帮助其他人。

#2


0  

If I understand your question, then I believe it can be solved by putting this at the top of your example.source. This will list your contents every-time that you cd.

如果我理解你的问题,那么我相信它可以通过将它放在example.source的顶部来解决。这将列出您每次cd的内容。

    #!/usr/bin/bash

    # Make cd change directories and then list the contents
    function cd() {
        builtin cd $*;
        ls;
    }

    mydir="/path/to/some/dir"
    cd $mydir

My other suggestion is to try to put cd within your alias. Something like this:

我的另一个建议是尝试将cd放在你的别名中。像这样的东西:

    mydir="cd /path/to/some/dir"
    $mydir

#1


1  

Apparently this feature is starting to be implemented in bash 4.3, release 26-Feb-2014 09:25.

显然这个功能开始在bash 4.3中实现,发布时间为2014年2月26日09:25。

Reading the NEWS file in bash 4.3 I found this:

在bash 4.3中阅读NEWS文件我发现了这个:

i. The word completion code checks whether or not a filename containing a shell variable expands to a directory name and appends `/' to the word as appropriate. The same code expands shell variables in command names when performing command completion.

一世。单词完成代码检查包含shell变量的文件名是否扩展为目录名,并在适当的位置附加“/”。执行命令完成时,相同的代码会扩展命令名中的shell变量。

Unfortunately I cannot do a de novo installation of bash (because I'm working on a server) but I hope this can help others.

不幸的是我无法重新安装bash(因为我在服务器上工作),但我希望这可以帮助其他人。

#2


0  

If I understand your question, then I believe it can be solved by putting this at the top of your example.source. This will list your contents every-time that you cd.

如果我理解你的问题,那么我相信它可以通过将它放在example.source的顶部来解决。这将列出您每次cd的内容。

    #!/usr/bin/bash

    # Make cd change directories and then list the contents
    function cd() {
        builtin cd $*;
        ls;
    }

    mydir="/path/to/some/dir"
    cd $mydir

My other suggestion is to try to put cd within your alias. Something like this:

我的另一个建议是尝试将cd放在你的别名中。像这样的东西:

    mydir="cd /path/to/some/dir"
    $mydir