3

I have a function defined in my .bashrc that I would like to bypass:

function func() {
   // func
}
export -f func

When I run env -i func I can access the func command without the function in the way, but if I try "func" or \func then I don't have any luck.

I read on another post that \ should work to bypass bash functions, is this true? If so, is there any reason that I am not able to use it in this case?

2
  • 1
    Did env -i func really work for you? env expects an external command, not your shell function.
    – cuonglm
    May 24, 2016 at 3:41
  • @cuonglm I think the OP means the external command "func". May 24, 2016 at 9:09

4 Answers 4

3

The official way to prevent function definitions from being used by the shell is to call:

command func

See: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/command.html

3

You could use the pathname of your external command "func", e.g. /usr/bin/func.

2

Using a backslash or other forms of quoting on the name of a function has no effect. In the shell's syntax analysis, functions are looked up long after quotes have been parsed.

You or the person who wrote what you read is confusing functions and aliases. Aliases are parsed much earlier, and quoting any part of a command name makes it ineligible for alias lookup. Thus \func forces func to be a function, builtin or external command, but not an alias.

To force a command name to be interpreted as a builtin or external command, use command func. To force a command name to be interpreted as an external command, use env func.

1

According to bash's documentation,

name

A word consisting solely of letters, numbers, and underscores, and beginning with a letter or underscore. Names are used as shell variable and function names. Also referred to as an identifier.

word

A sequence of characters treated as a unit by the shell. Words may not include unquoted metacharacters.

and it finds function names using words:

If the command name contains no slashes, the shell attempts to locate it. If there exists a shell function by that name, that function is invoked as described in Shell Functions.

pointing out in the section on Quoting (similar to POSIX):

Quoting is used to remove the special meaning of certain characters or words to the shell. Quoting can be used to disable special treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expansion.

Like POSIX, bash mentions quoting versus aliases, but neither specifically mention functions in regard to quoting.

In summary, there is no documented feature for quoting function names. If you want to ensure that you use an external command, the use of slashes is the way to go. bash and POSIX agree on that:

If a simple command results in a command name and an optional list of arguments, the following actions shall be performed:

  1. If the command name does not contain any <slash> characters, the first successful step in the following sequence shall occur:

It helps to read the documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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