Manage several shell profiles and switch between them.
Because:
- We all need to manage our shell session by:
- We may require to change those specific settings according to a given environment (e.g., by working in several contexts/clients), and thus need to manage several shell environments, or profiles, in parallel
- The more different environments we have, the more complicated it is to maintain our shell profile files (
.bashrc
,.bash_profile
,.zshrc
...) - We want to backup our shell session settings by focusing only on our own settings
- Manage different shell session settings, or profiles
- Unclutter shell session settings by modularizing the configuration and be able, for instance, to define 1 script file for 1 configuration/tool type and easily import or export them
- Apply a lexicographical order when discovering shell profiles' scripts
- Allow to define loading and unloading shell profile script types to handle transition between profiles
- Remember the current profile in use to be able to quickly reload it
shprofile
manages a set of shell profiles which can be enabled at any time. Scripts are executed within the current shell session, so scripts can modify the current shell environment.
Each shell profile is defined by a set of scripts contained into and associated folder from the $HOME/.shprofile/profiles
profiles root folder.
For instance:
$HOME/
.shprofile/
profiles/
myfirstprofile/
script1.sh
script2.sh
mysecondprofile/
script3.sh
script4.sh
defines two profiles, myfirstprofile
and mysecondprofile
, containing respectively the script1.sh
, script2.sh
and the script3.sh
, script4.sh
scripts.
Once profile is defined, it can be simply loaded via:
$ shprofile myfirstprofile
and be easily switched by another one via:
$ shprofile mysecondprofile
The current loaded profile is kept in memory (more precisely written into a file) to be quickly reloaded if necessary. The reload of the current profile can be done by calling shprofile
without a profile name.
Thus,
$ shprofile
will reload the current loaded profile.
Note that this feature can be useful if a specific configuration needs to be executed at any shell session openings. See further for more details.
Each script is a shell script and can be anything you want: exporting variables, setting the PATH
, applying a complex initialization process... All scripts are executed within the current shell session.
The name of a script is important. Depending on its name, a script is executed differently.
Scripts are discovered by using the lexicographical order. Then, if you want to execute script1.sh
before another one, a good practice is to use a numerical prefix in its name:
1-script1.sh
There are two types of scripts:
- Loading scripts (by default)
- Unloading scripts
A script is by default a loading script. That is: executed when a profile is loading.
But sometimes it may be necessary to run scripts during the transition to another profile. In this case, there are unloading scripts. An unloading script is suffixed by the keyword -unload
:
script2-unload.sh
Of course, execution order and execution type can be combined.
Let's take an example:
$HOME/
.shprofile/
profiles/
myfirstprofile/
1-script1.sh
1-script1-unload.sh
script2.sh
script2-unload.sh
mysecondprofile/
script3.sh
script4.sh
In this scenario, the 1-script1-unload.sh
script will be executed when leaving the myfirstprofile
profile, and before executing the script2-unload.sh
script.
Any dot file is ignored. So to disable execution of a script, just prefix it by the .
character.
$ mkdir -p $HOME/.shprofile/profiles
$ curl -o $HOME/.shprofile/shprofile.sh https://raw.githubusercontent.com/abourdon/shprofile/4.8/shprofile.sh
$ alias shprofile='source $HOME/.shprofile/shprofile.sh'
-
Now you can create your first profile by creating its associated folder within
$HOME/.shprofile/profiles
:$ mkdir $HOME/.shprofile/profiles/myfirstprofile
-
Add your desired scripts into it (some examples can be found here)
-
And finally enable it
$ shprofile myfirstprofile
A common use is to load the current profile at each shell session opening. Depending on your shell, this enabling can be done differently.
$ echo "alias shprofile='source $HOME/.shprofile/shprofile.sh'" >> $HOME/.bashrc
$ echo 'shprofile' >> $HOME/.bashrc
$ echo "alias shprofile='source $HOME/.shprofile/shprofile.sh'" >> $HOME/.zshrc
$ echo 'shprofile' >> $HOME/.zshrc
$ shprofile --help
Some examples of shell profile's scripts can be found here.
shprofile
can be seen as a combined version of /etc/profile.d
(because of its modular architecture) and .bash_profile
(because focusing on a single user), by adding the ability to:
- define several profiles
- not being constrained to use a shell type specific user profile file (e.g.,
.bash_profile
or.zprofile
)
Some alternatives to shprofile
could be:
Contributions are welcome :-) To do so, check out the instructions.
Copyright (c) 2021 Aurélien Bourdon
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.