How to do user-preset login to Bash shell then automate path modification?

How do a user login with full user-environment preset to Bash shell then automatically do path modification with few script codes, either on command-line or put it in a script file.
what i tried:

bash --login -c PATH="/ANewPath:${PATH}"
bash --login -c 'PATH="/ANewPath:${PATH}"; export PATH'
bash --login -c 'export PATH="/ANewPath:${PATH}"'
bash --login ~/ChangePath.sh

with no avail

Anybody please usefully and sincerely help me...
Thanks in advance

When a bash shell starts several scripts are executed automatically. You can put whatever commands you want to have executed automatically there.

First, a (general) script is executed: /etc/profile . This is done for every user and therefore it should contain only settings/actions which should be equal for every user logging on.

Next a user-specific script is called: ~/.bash_profile (if the shell is called as bash ) or ~/.profile (if the shell is called as sh ). If you do not want to differentiate between these two you can create a symbolic link from one to the other.

These two files are executed for so-called "login-shells": if you connect to a system, identify yourself as a certain user, then a login-shell is started. Which one that is is determined in /etc/passwd , where the attributes of all users are stored. Here is an example line from a Linux system:

root:x:0:0:Superuser:/root:/bin/bash

It says that the user root needs to identify with a passwort (the x - the password itself is stored elsewhere), has the user ID 0, its primary group has the group ID 0, the users description is Superuser , its home directory is /root and its login shell is /bin/bash .

There are other (non-login) shells, which are i.e. started when you start an xterm (or konsole or another terminal emulator) in a graphical environment. These shell instances do NOT execute the files mentioned before but they read and execute a file called ~/.bashrc .

So, here is a simple what-is-where:

/etc/profile Put in there whatever every user should have set/executed.

~/.profile Put in there what the respective user should have executed upon login. To set the login shells environment to the same as every other shells environment (if this is desired) add as the last line of this script the lines:

ENV=~/.bashrc
. ~/.bashrc

This will execute ~/.bashrc for every login script. Notice the dot in front!

~/.bashrc     # WRONG
. ~/.bashrc   # CORRECT

This means the script is executed not in a subshell but in the same shell! Otherwise whatever you would set in there would be lost when the script ends!

~/.bashrc Put everything in there what you want to have executed in every shell. This file is also on a per-user basis. Notice that you already have a certain environment in a shell once it gets to executing this script. i.e. you want to do:

export PATH="$PATH:/some/added/path"

because if you do this:

export PATH="/some/added/path"

Whatever the PATH was before it would be reduced to the one directory mentioned.

I hope this helps.

bakunin

1 Like

I'm not quite sure what you are trying to achieve. I think you are wanting to customise the environment by settings the PATH, variables, current directory etc. You would be better writing a script with the settings in and the you can source it.

Something like:-
my_env_1

PATH=/my_path1:$PATH
cd /my_dir1
BASE_VAR="DEV1"

my_env_2

PATH=/my_path2:$PATH
cd /my_dir2
BASE_VAR="DEV2"

etc.

You can then (from the shell prompt or within a sh/ksh/bash script) do source my_eny_1 or shorthand, you could . my_env_2 and it will set the various variables/values/directory etc. in the current shell. You could even (in .bashrc perhaps) create an alias to call this in for you, e.g. alias DEV1='source my_env_1 would allow you to just type DEV1 on the command line to set it up.

You need to be careful because switching back & forth between these two would grow the PATH variable each time, possibly leading to confusion.

Does that help, or have I missed the request entirely?

Robin