-5
  1. Is any alias equivalent to a function, in the sense that each appearance of the alias can be replaced with the function's name, and the function name in each call to the function can be replaced with the alias?

  2. If I am correct, an arbitrary alias is defined in the following form:

    alias myalias=blahblah
    

    Is the alias defined in the above form always equivalent to a function defined as

    myfun () { blahblah $@ }
    

    ?

    If not, what function is the alias equivalent to?

Thanks.

closed as unclear what you're asking by Mikel, Stephen Rauch, Scott, Archemar, Romeo Ninov Aug 4 '17 at 11:29

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Both that alias and function give me an error. – Jesse_b Aug 4 '17 at 2:39
  • I would think the following are identical (or pretty much any one line command): alias test1="echo Hello" test2(){ echo Hello ; } – Jesse_b Aug 4 '17 at 2:43
  • What is unclear about what I am asking? If you don't know it, would you rather leave the discussion open to others, than abuse your closing vote? Thanks. – Tim Aug 4 '17 at 12:35
  • 2
    As a fairly beginner *nix user a lot of your recent questions have been pretty confusing/unclear to me. That being said I do not understand why people are downvoting them, I have learned something from most of your questions and the answers to them and the lack of understanding is due to my knowledge of the subject not your question(s). – Jesse_b Aug 4 '17 at 12:38
  • Thanks for your kind word. @Jesse_b. I am happier now, because I know there is at least one who is open minded and capable of mutual understanding. – Tim Aug 4 '17 at 12:40
5

As the fine manual tells us, aliases are almost completely superceded by functions. Functions can do pretty much anything an alias can do, and are capable of doing a lot more as they take arguments which can be used in arbitrary order.

The thing that functions can't do is prevent the expansion of their arguments. This means that the only reason for using an alias is to set up a function call without expansion.

alias funny='set -f; _funny'
_funny(){ set +f ; do_something_with_unexpanded_args ;}

and now you can run funny * and see the * rather than a list of the files in the current directory.

1

NO function is always equivalent to a given alias, since::

$ alias foo=bar
+ alias foo=bar
$ myfun () { bar "$@"; }

Now, let us use foo and myfun to define a function:

$ foo () { blah "$@"; }
$ myfun () { blah "$@"; }

What happens to foo and myfun?

$ type -a foo bar myfun
+ type -a foo bar myfun
foo is aliased to `bar'
bar is a function
bar ()
{
    blah "$@"
}
myfun is a function
myfun ()
{
    blah "$@"
}

Clearly, the function that was a supposed equivalent of the alias got redefined, but the alias was unaffected.

For most other cases, myfun would have been an equivalent to foo.

Not the answer you're looking for? Browse other questions tagged or ask your own question.