tags:

views:

33

answers:

3

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.

+5  A: 
login@host ~$: . ./script
login@host ~$: mycmd

Will execute it in your shell I believe.

Wrikken
It works, thanks.
sigo
A: 

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

Jonathan Leffler
It's really big workaround.
sigo
@sigo: your call; I'd rather do it that way than using (new-fangled things like) aliases. I have one alias not set by the shell itself; that provides me with the 'r' (for 'repeat') command that was available in 'ksh'. Anything else is a script in my bin directory.
Jonathan Leffler
A: 

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

source myscript.sh
mycmd
ustun