23

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:

$ 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.

24

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.

  • 1
    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 '16 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 '16 at 18:23
  • Do you need to add #!/bin/sh to the top of .profile to define a function? – Kolob Canyon Oct 18 '16 at 16:49
13

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

  • +1 for export, I had to invoke and alias in ssh ProxyCommand, and it kept cribbing about alias not found even after source .profile. Works like a charm after export. – Abhishek Anand Jul 15 '18 at 3:43
7

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.

1

csh and tcsh doesn't support functions like bash but you can do it.

For example here's a simple function to print some text

alias print_hello_world 'eval echo "hello world"'

passing arguments to the script

repeate_after_me 'eval echo "$1"'

doing multi line code like an if statement is kinda complicated but here you go.

alias X 'eval "if (\!:1 =~ 'yes') then \\
          echo yes \\
          else \\
          echo no \\
          endif"'

you'll still have to eval but the most complex part is doing the \!:[x] syntax to get the input from the command line.

That should be enough to get you sorted out.

0

You can redirect all current functions to bashrc with typeset.

For example, here's how I'm adding a function of an openstack command with optional parameter, into bashrc:

urc() { . stackrc; set -x; openstack server list --all $1; set +x; }
typeset -f >> ~/.bashrc

Now it is saved for future sessions, and can be used like:

$ urc 

or

$ urc --debug
-1

point 18: In my older memory when an admin, learning ksh and bash was kina other way around. 1:- /etc/profile contents are sysWide. /etc/skel contained the file given to new accounts. Since Bash now prime common of all distro. the next would be 2:- ~/.profile as system standard then within contents of .profile "if exists" .bash_profile would put -> 3:- .bash_profile 4:- .bash_login 5:-.bashrc .bash_aliases would be in my mind the more correct order, setting up the standard Terminal and Shell environment and usual .bash_login never found or existing (its custom per user). With .profile $PATH being main if the .bash_profile its contents would also contain a given $PATH and there is its custom add by personal need for what additional Search PATH is to be added to system PATH if by chance your in need of an Installed APP in your path for quick access.
I'm finding in now adding too .bash_profile .profile is still dominating and not respecting the presence of .bash_profile. Along with header info no one is updating ie: bash startup Samples - that directory structure is no longer - along with examples and or files are gzip'd. So update to Gnu-Bash is in order

  • 1
    What are you going on about? – Jesse_b Jul 11 '18 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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