【问题标题】:Setting a pathname as an alias in the .bashrc file在 .bashrc 文件中将路径名设置为别名
【发布时间】:2015-03-28 03:31:03
【问题描述】:

我试图在 .bashrc 文件中使用别名来存储我经常去的路径(Ubuntu 14.04),即alias pathname="/home/Dommol/test/next" 但是当我尝试使用别名cd pathname 时,我收到错误-bash: cd: pathname: No such file or directory。

问题: 如何让 bash 识别出我正在尝试使用别名 pathname 而不是尝试更改为目录 pathname?

顺便说一句,我可以将别名设为alias pathname="cd /home/Dommol/test/next",然后输入pathname 进行更改

【问题讨论】:

    标签: bash


    【解决方案1】:

    alias 用于给命令起别名,而不是 shell 变量。做你想做的事,在你的.bashrc中设置一个shell变量:

    pathname="/home/Dommol/test/next"
    

    然后在提示符处:

    $ cd $pathname
    

    使用alias 在.bashrc 中使用您想要的参数创建自定义命令,正如您在“旁白”中所述:

    alias pathname="cd /home/Dommol/test/next"
    

    然后在提示符处:

    $ pathname
    

    【讨论】:

      【解决方案2】:

      lurker 的答案应该是公认的解决方案。但是,为了回答您最初的问题,我认为这可能有效:

      #Your test code:
      alias pathname="/home/Dommol/test/next"
      cd pathname
      
      #Similar functionality
      ln -s /home/Dommol/test/next pathname
      cd -P pathname
      

      如果您有更多这样的目录并且想从任何位置 cd,您可以这样:(请注意,下面的代码仅限于 cd 命令。)

      mkdir -p ~/.cdpath #Random name - could be changed
      export CDPATH=~/.cdpath
      
      ln -s /home/Dommol/test/next ~/.cdpath/pathname
      cd -P pathname #Will work from any starting location.
      
      ln -s /home/Dommol/test/next2 ~/.cdpath/pathname2
      cd -P pathname2 #Will work from any starting location.
      

      最佳解决方案是将其创建为变量,正如 lurker 的回答所解释的那样。该解决方案也适用于其他命令。

      【讨论】:

        猜你喜欢
        • 2018-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-14
        • 1970-01-01
        • 1970-01-01
        • 2018-08-22
        • 2023-04-04
        相关资源
        最近更新 更多