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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
After you define the function in your .profile, add export -f date1. This will export the function for use by your login shell.
Method 3
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.
Method 4
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.
Method 5
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
Method 6
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0