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

7
  • Both that alias and function give me an error. Commented Aug 4, 2017 at 2:39
  • I would think the following are identical (or pretty much any one line command): alias test1="echo Hello" test2(){ echo Hello ; } Commented Aug 4, 2017 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. Commented Aug 4, 2017 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). Commented Aug 4, 2017 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. Commented Aug 4, 2017 at 12:40

2 Answers 2

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.

2
  • Thanks. You raised a very good point, definitely worth my upvote. Commented Aug 4, 2017 at 15:38
  • unix.stackexchange.com/q/383972/674 Commented Aug 4, 2017 at 16:11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.