脚本中的全局 bash 别名
我想通过脚本创建别名,并在 bash 中使用它。
即:
#!/bin/bash
alias mycmd="ls -la"
Bash:
login@host ~$: ./script
login@host ~$: mycmd
*ls output*
当然,别名应该仅适用于一个会话(而不是 .bashrc 等)。 是否可以?不幸的是我还没有找到解决方案。
I'd like to create alias by script, and use it in bash.
Namely:
#!/bin/bash
alias mycmd="ls -la"
Bash:
login@host ~$: ./script
login@host ~$: mycmd
*ls output*
Of course, alias should be available just for one session (not .bashrc etc).
Is it possible? Unfortunately I have not found a solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信会在你的 shell 中执行它。
Will execute it in your shell I believe.
正确的方法是获取脚本而不是运行它。
The proper way to do is to source the script rather than running it.
创建一个脚本来完成这项工作,并将其放在 PATH 上的 bin 目录中(可能是 $HOME/bin):
如果您实际将 PATH 设置为包含 $HOME/bin,那么这是完全可靠的。
(当然,我们可以争论输入 5 个字母而不是 6 个字符的优点,但我认为这些名称是说明性的而不是真实的。)
Create a script to do the job, and put it in a bin directory on your PATH (probably $HOME/bin):
This is utterly reliable if you actually set PATH to include $HOME/bin.
(Of course, we can debate the merit of typing 5 letters instead of 6 characters, but I assume the names are illustrative rather than real.)