简体   繁体   English

使别名在终端会话中处于活动状态而不使用bashrc或bash_profile或bash_aliases?

[英]Having aliases active in a terminal session without using bashrc or bash_profile or bash_aliases?

This question is an offshoot of my question on whether there's anything wrong with having aliases on a production server . 这个问题是我的问题的分支,该问题是在生产服务器上使用别名是否有任何问题

So I tried creating a shell script with some aliases 所以我尝试用一​​些别名创建一个shell脚本

#!/bin/sh
echo "creating aliases..."
alias f='clear;cd ..;ls;pwd'
alias ff='clear;cd ../..;ls;pwd'

Did a chmod +x al.sh , and ran the script ./al.sh , but although the "creating aliases..." statement got printed, none of the aliases worked, because they were obviously active only until the script ran. 做了chmod +x al.sh并运行了脚本./al.sh ,但是尽管打印了“正在创建别名...”语句,但没有一个别名起作用,因为它们显然仅在脚本运行之前才处于活动状态。

So is there a way I can run a script containing the aliases I want, which will remain active as long as the terminal session is active? 那么,有没有一种方法可以运行包含我想要的别名的脚本,只要终端会话处于活动状态,这些脚本将保持活动状态? The basic idea being, not to cause problems for colleagues who use the same server. 基本思想是,不要对使用同一服务器的同事造成麻烦。

For cases when you want to store functions and aliases just for your session, I find it quite useful to have a file with them and sourcing it when I login the server. 对于只想为您的会话存储函数和别名的情况,我发现与它们一起存放文件并在登录服务器时进行采购非常有用。

So just place it somewhere like: 因此,只需将其放置在以下位置即可:

~/nav_alias_file.sh

And then just after ssh ing the server type: 然后在ssh服务器类型之后:

source ~/nav_alias_file.sh

Note by the way that, as Sundeep expressed in comments , you do not need the shebang in that file. 顺便提一下,正如Sundeep在评论中表示的那样 ,您不需要该文件中的shebang。

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

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