1

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.

0

3 Answers 3

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

Will execute it in your shell I believe.

Sign up to request clarification or add additional context in comments.

Comments

3

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

source myscript.sh
mycmd

Comments

1

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.