Take the 2-minute tour ×

I have the following code in my ~/.bashrc:

date=$(which date)
date() {
  if [[ $1 == -R || $1 == --rfc-822 ]]; then
    # Output RFC-822 compliant date string.
    # e.g. Wed, 16 Dec 2009 15:18:11 +0100
    $date | sed "s/[^ ][^ ]*$/$($date +%z)/"
    $date "$@"

This works fine, as far as I can tell. Is there a reason to avoid having a variable and a function with the same name?

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

It's alright apart from being confusing. Besides, they are not the same:

$ date=/bin/ls
$ type date 
date is hashed (/bin/date)
$ type $date 
/bin/ls is /bin/ls
$ moo=foo
$ type $moo 
-bash: type: foo: not found
$ function date() { true; }
$ type date 
date is a function
date () 
true*emphasized text*

$ which true 
$ type true
true is a shell builtin

Whenever you type a command, bash looks in three different places to find that command. The priority is as follows:

  1. shell builtins (help)
    • shell aliases (help alias)
    • shell functions (help function)
  2. hashed binaries files from $PATH ('leftmost' folders scanned first)

Variables are prefixed with a dollar sign, which makes them different from all of the above. To compare to your example: $date and date are not the same thing. So It's not really possible to have the same name for a variable and a function because they have different "namespaces".

You may find this somewhat confusing, but many scripts define "method variables" at the top of the file. e.g.


The common thing to do is type the variable names in capitals. This is useful for two purposes (apart from being less confusing):

  1. There is no $PATH
  2. Checking that all "dependencies" are runnable

You can't really check like this:

if [ "`which binary`" ]; then echo it\'s ok to continue.. ;fi

Because which will give you an error if binary has not yet been hashed (found in a path folder).

share|improve this answer
add comment

Since you always have to use $ to dereference a variable in Bash, you're free to use any name you like.

Beware of overriding a global, though.

See also:


share|improve this answer
add comment

An alternative to using a variable: use bash's command keyword (see the manual or run help command from a prompt):

date() {
    case $1 in
        -R|--rfc-2822) command date ... ;;
        *) command date "$@" ;;
share|improve this answer
That's a great approach. I didn't know about command. –  davidchambers Oct 6 '12 at 22:12
add comment

Your Answer


