Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

  date -d @$1

I'd like to be able to write:

$ date1 xxxyyy

Where xxxyyy is the parameter I pass into my function so I can get 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
up vote 7 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
Well I tried your way for creating a custom function of printing it's argument, but even if I add that function in .bash_profile then also I have to do source ~/.bash_profile everytime I open terminal by shortcut Ctrl+Alt+T. – Vicky Dev Jun 17 at 6:44
@VickyDev If you have to source ~/.bash_profile every time then that's an indication that your Bash setup is somehow non-standard or broken (as per the man bash output mentioned). Are you sure you're actually running Bash? For example, does echo $BASHPID print anything? – l0b0 Jun 17 at 18:23

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

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

Your Answer


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.