Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a function which converts epoch time to date. Here is the definition

date1(){
  date -d @$1
}

I'd like to be able to write:

$ data1 xxxyyy

And I want the corresponding date. I understand I have to add it in either .bash_profile, .profile, or .bashrc and then source it:

$ source file

But, I'm not sure which file to put it in. Currently, I have it in .profile. But to run it, I have to do source .profile every time.

Ideally, it should make it available, when the computer starts up like the environment variable.

share|improve this question
add comment

3 Answers 3

up vote 3 down vote accepted

From man bash:

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.

In other words, you can put it in any one of ~/.bash_profile, ~/.bash_login or ~/.profile, or any files sourced by either of those. Typically ~/.profile will source ~/.bashrc, which is the "personal initialization file, executed for login shells."

To enable it, either start a new shell, run exec $SHELL or run source ~/.bashrc.

share|improve this answer
add comment

After you define the function in your .profile, add export -f date1. This will export the function for use by your login shell.

share|improve this answer
add comment

Customizations for interactive shells go into ~/.bashrc. Things that you want to run when you log in go into ~/.profile (or ~/.bash_profile, but it's often not loaded when logging in graphically).

Put this function definition in ~/.bashrc.

Since bash doesn't load .bashrc when it's a login shell, force it to do so: write a ~/.bash_profile containing

. ~/.profile
case $- in *i*) . ~/.bashrc;; esac

i.e. load ~/.profile, and also load ~/.bashrc if the shell is interactive.

See Alternative to .bashrc and the posts linked there.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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