Totem
Totem

Reputation: 7369

Correct syntax for bash function as alias in .bashrc

I have just read the following instruction in a bash scripting tutorial:

Let's try one. Open .bashrc with your text editor again and replace the alias for "today" with the following:

today() {
    echo -n "Today's date is: "
    date +"%A, %B %-d, %Y"
}

The line I am supposed to replace is 'date +"%A, %B %-d, %Y"'

The full line being:

alias today='date +"%A, %B %-d, %Y"'

However, having tried the following:

alias today='today() { 
                echo -n "Today's date is: "
                date +"%A, %B %-d, %Y"
             }'

With and without the apostrophe on the 2nd line(in "Today's"), and w/wo the enclosing single quotes, as a one liner, and using the function keyword combined with all the other options listed. I also tried to define the function above the alias statement and then simply use 'today()'(w/wo quotes) as the alias value, as a long shot. None of the above is working.

What is the correct syntax here to use this function as the alias?

Upvotes: 0

Views: 160

Answers (2)

Etan Reisner
Etan Reisner

Reputation: 81052

That snippet isn't telling you to do anything with the alias but delete it.

It is trying to have you replace the alias with a function since functions are generally more useful than aliases.

Upvotes: 2

anubhava
anubhava

Reputation: 786091

You can have it separately:

_today() { echo -n "Today's date is: "; date +"%A, %B %-d, %Y"; }
alias today='_today'

Upvotes: 2

Related Questions