eva*_*vid 2 bash alias profile
有没有办法从命令行添加别名而不直接编辑 bash_aliases 文件?澄清一下,如何让 bash 脚本执行此操作,而无需输入类似的内容nano bash_aliases来添加它们?
假设您正在使用bash_aliases(这不是必需的,您也可以在.bashrc其他地方定义别名),您可以简单地在文件中添加一行:
printf "alias foo='bar'" >> ~/.bash_aliases
Run Code Online (Sandbox Code Playgroud)
或者,如果您只想为当前会话使用此别名,请直接使用 alias 命令:
alias foo='bar'
Run Code Online (Sandbox Code Playgroud)
默认情况下,Bash 不允许在脚本中扩展(工作)别名,您需要激活该expand_aliases选项:
#!/usr/bin/env bash
alias foo='echo "It works!"'
echo " Alias defined, attempting to use without expand_aliases"
foo
shopt -s expand_aliases
echo " Attempting to use with expand_aliases"
foo
Run Code Online (Sandbox Code Playgroud)
如果我运行上面的脚本,别名foo仅在我激活该expand_aliases选项后才起作用:
$ a.sh
Alias defined, attempting to use without expand_aliases
/home/terdon/scripts/a.sh: line 5: foo: command not found
Attempting to use with expand_aliases
It works!
Run Code Online (Sandbox Code Playgroud)