开发者

Global bash alias from script

开发者 https://www.devze.com 2023-01-16 23:26 出处:网络
I\'d like to create alias by script, and use it in bash. Namely: #!/bin/bash alias mycmd=\"ls -la\" Bash:

I'd like to create alias by script, and use it in bash.

Namely:

#!/bin/bash
alias mycmd="ls -la"

Bash:

login@hos开发者_StackOverflow社区t ~$: ./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.


login@host ~$: . ./script
login@host ~$: mycmd

Will execute it in your shell I believe.


The proper way to do is to source the script rather than running it.

source myscript.sh
mycmd


Create a script to do the job, and put it in a bin directory on your PATH (probably $HOME/bin):

$ echo "exec ls -la \"\$@\"" > $HOME/bin/mycmd
$ chmod 555 $HOME/bin/mycmd
$

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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消