Hacker News new | ask | show | jobs
by torbjorn 3125 days ago
What the author says about aliases in the bashrc being evaluated only once when the bashrc is run when the shell session is opened is wrong.

Go ahead and try this:

Put,

    alias lb='vim ~/logbook/$(date "+%s")'
in your bashrc.

Source the bashrc.

Run the lb command. It creates a new file with a different epoch timestamp each time. This `lb` command doesn't need to be a bash function. It can be an alias and work just fine.

Maybe how this works in zsh is different...

3 comments

Zsh is different, although I'd be surprised if this wasn't the Bash behavior.

It doesn't make sense. If you do

    alias e=vim
it's not like Vim opens when your shell starts.

The only way to have a "precomputed" alias is to get the quoting wrong, and leave $() in double quotes instead of single quotes, meaning it will be evaluated before the alias is even assigned.

Their function uses single quotes on format string, so I'll bet they tried it like this:

   alias lb="vim ~/logbook/$(date '+%Y-%m-%d').md"
..which would do what was described.
Seeing as the author is using vim he could just have a command within vim.

  :exec ':e ~/logbook/' . strftime("%Y-%m-%d.md")