简体   繁体   English

仅打印命名目录的别名?

[英]An alias that just prints the named directory?

I'm kind of just realizing how powerful the terminal can be. 我只是在意识到终端的功能强大。 My question is essentially if I can create an alias that just prints the name of a directory. 我的问题本质上是,是否可以创建一个仅显示目录名称的别名。 For example, I could easily make an alias such as "alias sitename="cd ~/sites/path/to/my/site/". But what I want is an alias that only prints the directory name so that I can use it for several things. So that, for example, if I wanted I could just say cd "alias", or mv from-dir "alias". 例如,我可以轻松地创建一个别名,例如“ alias sitename =“ cd〜/ sites / path / to / my / site /”。但是我想要的是一个仅打印目录名称的别名,以便我可以使用它例如,如果需要,我可以说cd“ alias”或mv from-dir“ alias”。

Is there a way to do this? 有没有办法做到这一点? I've tried and it seems to recognize the alias if I just type it in: it will report "alias" is a directory. 我已经尝试过了,如果我只是键入别名,它似乎可以识别该别名:它将报告“ alias”是一个目录。 But if I try to couple it with another command, it fails. 但是,如果我尝试将其与另一个命令结合使用,它将失败。

Use a variable, simply 简单地使用变量

d=/path/to/some/directory
echo $d
cd $d
mv somedir $d/

You don't need to use an alias here, a variable is sufficient. 您无需在此处使用别名变量就足够了。

You don't want to use alias , what you are after is an environment variable 您不想使用alias ,所追求的只是一个环境变量

$ export SITENAME="~/sites/path/to/my/site/"
$ cd $SITENAME

Bash is quite picky over syntax - note the lack of spaces in the export and the $ when you use it. Bash对语法非常挑剔-请注意,导出时和$时都没有空格。

It sounds like you want to set a variable, not an alias. 听起来您想设置一个变量,而不是别名。 Such as, sitename=/home/jimbo/ . 例如sitename=/home/jimbo/ Then, cd $sitename would put you at /home/jimbo/ . 然后, cd $sitename将把您放在/home/jimbo/

If you want this variable to have permanence (ie you don't have to set it every time you open a new session), then you can make it an environmental variable using the export command or add it to your .bashrc file (typically located at $HOME/.bashrc ) using the line: sitename=/home/jimbo/ . 如果您希望此变量具有永久性(即,不必在每次打开新会话时都进行设置),则可以使用export命令将其设置为环境变量,或将其添加到您的.bashrc文件中(通常位于在$HOME/.bashrc ),使用以下行: sitename=/home/jimbo/

FYI, $HOME is another environmental variable that's equivalent to ~/ . 仅供参考, $HOME是另一个等同于~/环境变量。

Using a variable is the simplest solution. 使用变量是最简单的解决方案。 You could get fancy and use an array: 您可能会喜欢上并使用一个数组:

mydir() { echo "/my/directory"; }

To display the value 显示值

mydir

To use the value, you need some extra puncuation 要使用该值,您需要一些额外的标点符号

cd $(mydir)
cd `mydir`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM