How can I add these lines automatically using a script.
If I run the script, then it automatically inserts the lines at the end of .bashrc file.
and Remove duplicate $PATH entries with awk command to avoid adding duplicates or remove them. PATH is just an environment variable, and if you want to add to the PATH, you have to rebuild the variable with exactly the contents you want.
That is, what you give as an example to question 2 is exactly what you want to do, unless I'm totally missing the point of the question. I have a generic profile that I install on every machine I work on that looks like this, to accommodate for potentially-missing directories: export PATH=/opt/bin:/usr/local/bin:/usr/contrib/bin:/bin:/usr/bin:/usr/sbin:/usr/bin/X11 # add optional items to the path for bindir in $HOME/local/bin $HOME/bin; do if [ -d $bindir ]; then PATH=$PATH:$ fi done Inclusion basically incorporates the "called" script in the "calling" script. So it's effective inside the "calling" script or program.
There's already an accepted answer here, but you might consider using some form of environment modules to handle system-wide configuration of user environments rather than messing with the files in /etc/profile.d, etc.
This is especially true if you want to manage this control in one place across lots of shells.
Following is a link to this page's forum counterpart where you can post any related feedback and get more information about aliases:  Credits goes to Handy for his .bashrc guide.
When you sign in to comment, IBM will provide your email, first name and last name to DISQUS.
@High Commander4 - a very unsatisfactory way to sort of do what you want is to do "bash -l" however this actually creates a new subshell and when you logout you'll return to the enclosing shell where "foo" is still in PATH.
If you're just interested in PATH, you could do "unset PATH" and reconstruct it from scratch, but probably easier/safer is to do "PATH=/bin:/usr/bin" before sourcing your .bashrc.
Since my computer is only used by me, I put any directories that I want in my path there, unless it is a temporary addition that I put in a script.
This (Bash-only) function does the "right thing" in the above situations (with an exception, see below), returns error codes, and prints nice messages for humans.
during my work I need to constantly add alias commands to bashrc, most of those commands needs to be runed by other users.