Is it possible to use the "@" symbol as a function name in a bash script? The following does not work:
function @() {
echo hello
}
From man bash
name A word consisting only of alphanumeric characters and underscores,
and beginning with an alphabetic character or an underscore. Also
referred to as an identifier.
Shell functions are declared as follows:
name () compound-command [redirection]
function name [()] compound-command [redirection]
@.
EDIT 2: While @ is unproblematic in vanilla bash, it is used as a pattern grouping operator when the extglob shell option is set, a simple echo @() can hang your shell under certain conditions. All the more reason to not use @ as an identifier`.
EDIT 1: To clarify, it is not allowed per the definition of an identifier. I'm not recommending it. I'm just saying that for backwards compatibility, it is possible in bash to use @ as an identifier.
@() { echo "$1 world"; }
@ hello
alias @="echo hello"
@ world
@ is used as a special parameter ($@) and for arrays (${arr[@]}) but nowhere else. Bash won't stop you using it for identifiers like aliases and functions but not variable names.
Some people alias @ to sudo, so they can execute commands as @ apt-get install.
The main reason why I would not use @ as a shell identifier is that I'm so used to the semantics of @ in Makefiles which silences commands without any side effects.
BTW: Works the same in zsh.
4.3.11(1)-release (Ubuntu 14.04, bash package 4.3-7ubuntu1.5) and on 4.3.42(1)-release (Arch Linux, package 4.3.042-4), after entering @() { echo "$1 world" and pressing enter, I get {: command not found.
The naming of functions is quite similar to the allowed characters for alias:
From man bash:
The characters /, $, `, and = and any of the shell metacharacters or quoting characters listed above may not appear in an alias name.
metacharacter
One of the following: | & ; ( ) < > space tab
So, except for: / $ ` = | & ; ( ) < > space tab
any other character should be valid for alias and function names.
However, the character @ is also used for @(pattern-list) when extglob is active. By default extglob is active in interactive shells.
So, this should fail:
$ @(){ echo "hello"; }
bash: syntax error near unexpected token `}'
However, this works:
$ bash -c '@(){ echo "hello"; }; @'
hello
And this should work as well:
$ shopt -u extglob ### keep this as an independent line.
$ @(){ echo "hello"; }
$ @
hello
It is also possible to do this:
$ f(){ echo "yes"; }
$ alias @=f
$ @
yes
An alias is flexible in what symbols it accepts.
4.3.42(1)-release(Arch Linux, package4.3.042-4), the function definition as given in the question doesn't give an error, but running@gives@: command not found. Ditto on4.3.11(1)-release(Ubuntu 14.04, bash package4.3-7ubuntu1.5).export -f @givesexport: @: not a function.extglob. Never mind.