0

I have to create the function in shell script, that function name must contain the special characters. like

     >()
      {
       echo $1 $2
      }

Here my function name is >, If its possible how can I give, Like above I tried that But it will through the error

Bhuvanesh
  • 3,365
  • 3
  • 13
  • 7
  • 3
    This is probably an XY problem. Why do you need a function called >? – jofel Mar 25 '15 at 10:20
  • @jofel I need to add the function in bashrc file, when redirection symbol is occur on my terminal window I need to call that function. – Bhuvanesh Mar 25 '15 at 10:24
  • We are getting closer. Can you elaborate more? Do you need this for debugging? bash accepts only one argument after > so why do you use $1 and $2? The answer to your actually question is probably actually no, but it is not yet clear what you really want to achieve. – jofel Mar 25 '15 at 10:31
  • Please [edit] your question and explain what your final objective is. What exactly do you want to achieve with this function? Calling a function > is not a solution, if you explain what you are trying to do, we might be able to help you find another way. – terdon Mar 25 '15 at 12:52
  • Learning is like the sky... it never ends.;) – mirekphd Aug 06 '23 at 08:52

2 Answers2

2

Most shells restrict the name of functions to contain only characters that don't need to be quoted, which excludes >.

Even in the few shells that allow > as a function name (I only know of zsh), defining a function called > would only have an effect if you called > as a command (which would require quoting it, i.e. running \> or ">" or '>'). It would not change how redirection works.

None of the usual shells have a way to configure what happens if some subsequent part of the script performs a redirection. If you need that kind of things, a shell is not nearly a flexible enough language. But there's probably a better way to solve whatever problem you're trying to solve — and quite possibly one that can be solved in a shell script.

1

The > has a special meaning in the shell, it's a redirection operator, see the manpage:

[n]> file Redirect standard output (or n) to file.

The name of a function can contain only letters (a-z or A-Z), digit (0-9) or the underscore character (_). Also is must not begin with a digit. (see Function Definition Command and Name)

chaos
  • 48,171