yatici
yatici

Reputation: 577

alias maybe shouldn't be an alias?

I am pretty new to creating aliases and functions at bash so I want something really simple in a nutshell

alias edit='emacs 'argv[0]'&'

so I can do edit junk.txt

to edit it on the background,but not sure if I actually need to create a function for something like this. This is probably way too obvious for unix people.

Upvotes: 1

Views: 80

Answers (3)

Keith Thompson
Keith Thompson

Reputation: 263487

You can't do what you're trying to do with an alias, at least not in bash.

Quoting the bash document:

There is no mechanism for using arguments in the replacement text, as in csh. If arguments are needed, a shell function should be used (see Shell Functions).

An alias can only be used as the first word of a simple command. There's no way to expand edit foo.txt to emacs foo.txt &.

(csh (and tcsh) aliases are more powerful, but csh lacks functions -- and you're using bash anyway.)

Note that the name argv, which you used in the attempt in your question, doesn't mean anything in particular in bash. Arguments (to a script or to a function) are referred to as $1, $2, etc.; the complete list of arguments is $* or $@. But that doesn't apply to aliases. The bash document explains all this.

glglgl's answer is correct: use a function.

Upvotes: 0

anubhava
anubhava

Reputation: 785481

Just use this alias as:

alias edit='emacs '

Note a space before quote that will let you pass arguments directly to emacs command and then you can call this alias as:

edit junk.txt

OR

edit foo.sh

Upvotes: 1

glglgl
glglgl

Reputation: 91099

Use a function, not an alias:

edit() {
    emacs "$@" &
}

Upvotes: 2

Related Questions