Mateusz Jagiełło
Mateusz Jagiełło

Reputation: 7154

Global bash alias from script

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.

Upvotes: 1

Views: 3033

Answers (3)

ustun
ustun

Reputation: 7041

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

source myscript.sh
mycmd

Upvotes: 3

Jonathan Leffler
Jonathan Leffler

Reputation: 754780

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

Upvotes: 1

Wrikken
Wrikken

Reputation: 70540

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

Will execute it in your shell I believe.

Upvotes: 5

Related Questions