Files
Modular-Bashrc-Manager/README.md

565 lines
13 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Modular Bashrc Manager
### A simple, modular, and clean way to manage your `.bashrc` file in Linux.
---
`Modular Bashrc Manager` is a shell-based system designed to keep your Bash configuration clean, structured, and easy to maintain.
Instead of placing everything directly inside `.bashrc`, this project lets you split your configuration into modular scripts that are loaded automatically at shell startup.
The project includes:
- an installer
- a command-line management tool
- a modular directory layout
- default ready-to-use scripts
- a reusable colored echo utility
This makes it easier to add, remove, enable, disable, and maintain shell customizations without turning `.bashrc` into a long and messy file.
---
## Features
- **Modular setup**
Keep `.bashrc` organized by loading only the scripts you need.
- **Script management**
Create, enable, disable, remove, and modify scripts from a single command interface.
- **Automatic directory structure**
The installer creates all required folders for a modular Bash environment.
- **Default scripts included**
Three scripts are installed by default so users can start immediately.
- **Easy integration**
The project integrates with an existing Bash setup without requiring a full rewrite.
- **Reusable utility functions**
Includes `ccecho`, a colored echo helper for cleaner terminal output.
- **Clean startup behavior**
Only required scripts are loaded at shell startup, keeping startup logic predictable.
---
## How It Works
The system adds a small loader block to your `.bashrc`.
That block sources scripts from the modular directories:
- `scripts-needed/`
Essential scripts required for the system to work.
- `scripts-enabled/`
Scripts that should be loaded at shell startup.
- `scripts-available/`
Scripts that are present but not automatically loaded.
- `scripts-removed/`
Scripts that were removed and stored as backups.
The idea is simple: keep `.bashrc` minimal, and move all additional Bash logic into dedicated modular files.
---
## Directory Structure
After installation, the system uses the following layout:
```text
~/.bashrc.d/
├── scripts-needed/
│ ├── brc-script.sh
│ └── ccecho.sh
├── scripts-available/
│ ├── 00_default.sh
│ ├── 01_git-cli-highlitgh.sh
│ └── 02_bashboard.sh
├── scripts-enabled/
└── scripts-removed/
```
### Folder Purpose
#### `scripts-needed/`
Contains the core scripts required by the project itself. These are loaded automatically and should not normally be disabled.
#### `scripts-available/`
Contains scripts that are installed and ready to be enabled. They are available for use but are not necessarily loaded at shell startup.
#### `scripts-enabled/`
Contains the scripts that are actively enabled and sourced when Bash starts.
#### `scripts-removed/`
Stores removed scripts as backups, usually with timestamps, so they can be restored later if needed.
---
## Default Scripts Included
The installer now copies three scripts into `scripts-available/` by default.
These scripts are included to provide immediate value and to demonstrate how the modular system can be used in practice.
---
### `00_default.sh` Modular Bashrc Introduction and Migration Helper
This script is intended as a starting point for users who are moving from a traditional `.bashrc` file to a modular Bash setup.
It explains the philosophy of the project and reminds the user that the modular loader block has already been added to `.bashrc`.
#### Main purpose
- show a welcome message
- explain the modular system
- remind the user how to keep `.bashrc` clean
- provide a safe place to move old custom Bash configuration
#### What to put in this script
You can move the following content from your old `.bashrc` into `00_default.sh`:
- aliases
- functions
- exports
- prompt customizations
- shell variables
- other custom Bash logic that you want loaded automatically
#### Recommended workflow
1. Open your `.bashrc`
2. Copy your personal customizations from the old file
3. Paste them into:
```bash
~/.bashrc.d/scripts-available/00_default.sh
```
4. Enable the script using:
```bash
brc-script -e
```
#### Important note
Do **not** remove the modular loader block from `.bashrc`.
That block is required so Bash can load the modular scripts at startup.
#### Typical content of `.bashrc`
Your `.bashrc` should stay lightweight and contain mainly the modular loader block plus any absolutely necessary minimal settings.
A typical loader block looks like this:
```bash
# Modular Bashrc
mkdir -p ~/.bashrc.d/scripts-needed
mkdir -p ~/.bashrc.d/scripts-enabled
mkdir -p ~/.bashrc.d/scripts-available
if [ -d ~/.bashrc.d ]; then
for needed in ~/.bashrc.d/scripts-needed/*.sh; do
[ -r "$needed" ] && source "$needed"
done
unset needed
for file in ~/.bashrc.d/scripts-enabled/*.sh; do
[ -r "$file" ] && source "$file"
done
unset file
fi
```
This keeps your shell configuration clean and maintainable.
---
### `01_git-cli-highlitgh.sh` Git-Aware Prompt Enhancement
This script improves the Bash prompt by showing useful Git repository information directly in the terminal.
It is especially useful for developers who frequently work inside Git repositories and want an immediate visual overview of the repository state.
#### What it does
The script defines a `parse_git_branch()` function that detects whether the current directory is inside a Git repository. If it is, it builds a status summary for the prompt.
#### Information shown in the prompt
- current branch name
- staged changes
- modified files
- untracked files
- remote ahead/behind status
#### Visual behavior
The script uses color-coded output to help distinguish repository states:
- **green** for a clean repository
- **purple** when there are uncommitted changes
- **cyan** for ahead/behind indicators
- additional colors for staged, modified, and untracked changes
#### Example prompt
```text
user@host:~/repo(main ↑1 +2 M:1 N:3)$
```
#### Notes
This script is useful for:
- developers
- DevOps users
- anyone working with Git directly in the terminal
---
### `02_bashboard.sh` Lightweight System Dashboard
This script displays a compact system dashboard directly in the terminal.
It is intended to give a quick overview of the machine without opening additional tools.
#### What it shows
- Linux distribution information
- hostname
- local IP addresses
- public IP address
- CPU usage
- RAM usage
- disk usage
- uptime
- load average
- available updates
- temperature, if available
#### Extra features
- Uses a cache directory in `/tmp` to avoid repeated network calls
- Avoids expensive update checks too frequently
- Can hide temperature information if the machine is a virtual machine
- Works best on Debian-based systems
#### Dependencies
This script requires:
- `curl`
- `lm-sensors`
You can install them with:
```bash
apt update && apt install curl lm-sensors
```
#### Practical use cases
This script is useful for:
- system administration
- quick health checks
- monitoring a remote box in a terminal
- showing a terminal dashboard at login or on demand
---
## Installation
The project can be installed using the included installer script.
### Automated Installation
1. Clone the repository:
```bash
git clone https://gitea.sld-server.org/sld-admin/Modular-Bashrc-Manager
cd Modular-Bashrc-Manager/installer
```
2. Make the installer executable and run it:
```bash
chmod +x brc-script-install.sh
./brc-script-install.sh
```
### What the installer does
The installer will:
- detect or ask which user should receive the installation
- create the modular directory structure
- back up the existing `.bashrc`
- append the required loader block to `.bashrc`
- copy the project files into `~/.bashrc.d/`
- copy the three default scripts into `scripts-available/`
- reload the shell configuration
3. Reload `.bashrc` if needed:
```bash
source ~/.bashrc
```
---
### Manual Installation
If you prefer to install the project manually, you can do it step by step.
#### 1. Create the directory structure
```bash
mkdir -p ~/.bashrc.d/scripts-needed
mkdir -p ~/.bashrc.d/scripts-enabled
mkdir -p ~/.bashrc.d/scripts-available
mkdir -p ~/.bashrc.d/scripts-removed
```
#### 2. Copy the core scripts into `scripts-needed/`
```bash
cp brc-script.sh ccecho.sh ~/.bashrc.d/scripts-needed/
chmod 750 ~/.bashrc.d/scripts-needed/brc-script.sh ~/.bashrc.d/scripts-needed/ccecho.sh
```
#### 3. Add the modular loader block to `.bashrc`
Append this block to the end of your `.bashrc` file:
```bash
# Modular Bashrc
mkdir -p ~/.bashrc.d/scripts-needed
mkdir -p ~/.bashrc.d/scripts-enabled
mkdir -p ~/.bashrc.d/scripts-available
if [ -d ~/.bashrc.d ]; then
for needed in ~/.bashrc.d/scripts-needed/*.sh; do
[ -r "$needed" ] && source "$needed"
done
unset needed
for file in ~/.bashrc.d/scripts-enabled/*.sh; do
[ -r "$file" ] && source "$file"
done
unset file
fi
```
#### 4. Copy the default scripts into `scripts-available/`
```bash
cp 00_default.sh 01_git-cli-highlitgh.sh 02_bashboard.sh ~/.bashrc.d/scripts-available/
ln -s ~/.bashrc.d/scripts-available/00_default.sh ~/.bashrc.d/scripts-enabled/00_default.sh
ln -s ~/.bashrc.d/scripts-available/01_git-cli-highlitgh.sh ~/.bashrc.d/scripts-enabled/01_git-cli-highlitgh.sh
ln -s ~/.bashrc.d/scripts-available/02_bashboard.sh ~/.bashrc.d/scripts-enabled/02_bashboard.sh
```
#### 5. Reload the shell
```bash
source ~/.bashrc
```
---
## Usage
The main management tool is `brc-script.sh`.
It can be used to manage scripts stored in the modular directories.
### Common commands
- `brc-script -c`
Create a new script in `scripts-available/`
- `brc-script -m`
Modify an existing script
- `brc-script -l`
List all available and enabled scripts
- `brc-script -e`
Enable a script from `scripts-available/`
- `brc-script -d`
Disable an enabled script
- `brc-script -r`
Remove a script and store it in `scripts-removed/`
### Example usage
```bash
brc-script -c
brc-script -l
brc-script -e
brc-script -d
brc-script -r
```
---
## How to Use `00_default.sh` Correctly
`00_default.sh` is the ideal place to move your personal Bash customizations.
### Move here from your old `.bashrc`
You can place inside `00_default.sh`:
- alias definitions
- functions
- environment variables
- exports
- shell helpers
- custom startup commands
### Keep out of `.bashrc`
The following should stay out of the main `.bashrc` file as much as possible:
- large alias blocks
- long function definitions
- extra startup logic
- custom prompts
- project-specific utilities
### Recommended structure
Your `.bashrc` should contain:
- the modular loader block
- only the minimum required shell settings
Everything else should live in modular scripts such as `00_default.sh`.
This approach makes your shell configuration:
- easier to read
- easier to debug
- easier to backup
- easier to share between machines
---
## Extra Utilities
### `ccecho` Colored Echo for Better Terminal Output
The project includes a reusable utility script called `ccecho.sh`, located in `scripts-needed/`.
It defines a `ccecho` function for printing styled and colored text to the terminal.
#### Example usage
```bash
ccecho -t green -b black -s bold "Success!"
ccecho -t red -s underline "Error!"
ccecho "Normal message without styling"
```
#### Available text colors
- `black`
- `red`
- `green`
- `yellow`
- `blue`
- `magenta`
- `cyan`
- `white`
- `bblack`
- `bred`
- `bgreen`
- `byellow`
- `bblue`
- `bmagenta`
- `bcyan`
- `bwhite`
#### Background colors
The same color names can be used with `-b`.
#### Styles
- `bold`
- `dim`
- `italic`
- `underline`
- `blink`
- `reverse`
- `hidden`
- `strike`
#### Make `ccecho` available in other sessions
If you want to use `ccecho` in other terminal sessions or scripts, source it from `.bashrc`:
```bash
source ~/.bashrc.d/scripts-needed/ccecho.sh
```
---
## Why Use This System?
Managing a large `.bashrc` file can quickly become difficult, especially when multiple aliases, functions, and startup commands are added over time.
This system solves that problem by separating your Bash configuration into small, focused files.
### Benefits
- **Organization**
Keep your shell configuration structured and easier to understand.
- **Maintainability**
Update one script without editing a huge `.bashrc` file.
- **Safety**
Removed scripts are backed up in `scripts-removed/`.
- **Flexibility**
Enable or disable features without rewriting your shell setup.
- **Reusability**
Share modular scripts across systems more easily.
---
## Contributing
Contributions are welcome.
If you find bugs, edge cases, or improvements, feel free to open an issue or submit a pull request.
Possible improvements include:
- support for more shells
- richer installer feedback
- logging and debug mode
- better script validation
- package-based installation
---
## License
This project is licensed under the GNU General Public License v3.0.
For more information, see the [GPL v3 license](https://www.gnu.org/licenses/gpl-3.0.html).