自动将别名添加到 .bashrc

发布于 2024-10-24 02:11:53 字数 331 浏览 2 评论 0原文

我知道可能没有简单的方法可以做到这一点,但我想我可以把它放在那里。我有一个目录,其中包含我所处理的每个客户的子目录。我希望能够在计算机上的任何位置键入该客户目录名称,然后切换到该目录。换句话说:

/dir/customers/
/dir/customers/customer1/
/dir/customers/customer2/
/dir/customers/customer3/

我希望将 customer1、customer2 和 customer3 全部添加到我的 ~/.bashrc 文件中,并且每当我创建新客户时,它也会更新以添加该文件。

有接受者吗?

I know there is probably no simple way to do this, but I thought I might put this out there. I have a directory that contains a subdirectory for each customer I deal with. I'd like to be able to type that customers directory name in, anywhere on the computer, and switch to that directory. In other words:

/dir/customers/
/dir/customers/customer1/
/dir/customers/customer2/
/dir/customers/customer3/

I'd like customer1, customer2, and customer3 to all be added to my ~/.bashrc file, and whenever I create a new customer, it would update to add that as well.

Any takers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生未歇 2024-10-31 02:11:53

如果您在 ~/.bashrc 中添加此代码:

for i in /dir/customers/*
do 
   alias $(basename $i)="cd '$i'"
done

它将为 customer1、customer2、customer3(/dir/customers/ 的所有子目录)设置别名,并且每次添加新客户(例如:customerN)时,它的别名为 customerN将在您登录时自动添加。

例如:别名 customer1 是 cd /dir/customers/customer1 ,别名 customer2 是 cd /dir/customers/customer2 等等...

If you add this code in your ~/.bashrc:

for i in /dir/customers/*
do 
   alias $(basename $i)="cd '$i'"
done

It will setup aliases for customer1, customer2, customer3 (all the sub directories of /dir/customers/) and every time you add a new customer (eg: customerN) it's alias customerN will be added automatically you log in.

eg: alias customer1 is cd /dir/customers/customer1 and alias customer2 is cd /dir/customers/customer2 so on...

在你怀里撒娇 2024-10-31 02:11:53

值得一提的替代方案:您可以export CDPATH=/dir/customers

,然后如果您在任何地方输入cd David_Johnson,您将被带到/dir/ customers/David_Johnson(如果存在这样的目录)。

这里的好处是,您可以只添加 David_Johnson 并且不需要为您的 .bashrc 提供资源来使其工作(我知道它不是别名,但似乎提供有点灵活性)

An alternative worth mentioning: you can export CDPATH=/dir/customers

and then if you are anywhere and you type cd David_Johnson you would be taken to /dir/customers/David_Johnson if such a directory existed.

The benefit here is that you could have just Added David_Johnson and did not need to resource your .bashrc for this to work (I know it's not an alias, but seems to offer a bit flexibility)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文