I don’t know bash scripting
if [ -d ~/.bashrc.d ]; then
for rc in ~/.bashrc.d/*; do
if [ -f "$rc" ]; then
. "$rc"
fi
done
I asked chatgpt and it said this is non standard? There is no bashrc.d directory on my home folder, I have uncommented the lines for now but dont know if this is benign or malignant
It first checks if
~/.bashrc.d
is an existing directory. If this it the case it then iterates over all entries in that directory. In this iteration it checks if the entry is a file and if this is the case it sources that file using the bash-internal shorthand.
forsource
.So it basically executes all scripts in
~/.bashrc.d
. This makes it possible for you to split your bash configuration into multiple files. This quite common and a lot of programs already support it (100% depends on the program, though).This is absolutely harmless as it is. But: if you or a program places anything in the directory
~/.bashrc.d
it WILL be sourced everytime you start a bash.A slightly better variant would be iterating over
~/.bashrc.d/*.sh
instead of just~/.bashrc.d/*
to make sure to only grab files with the.sh
suffix (even if suffixes are basically meaningless from a technical point of view) and also test for the file being executable (-x
instead of-f
).This would make sure that only files that are ending with
.sh
and that are executable are sourced. The “attack vector”, if you want to call it like that, would then be a bit more narrow than just placing a file in a directory.As for why it’s there: Did you ever touch your
.bashrc
? If not, maybe it is there since the beginning because it’s in the so-called skeleton (see/etc/skel/.bashrc
) that was used to initialize certain files on user account creation.Damn, now I want this too!
As others have said, all it does is pull in any other config files in that may be in that folder. And storing them separately like that is much better for backup and portability.
Ubuntu doesn’t do this.
I couldn’t say how it got there, but it’s a standard & common Unix pattern and unlikely to be malicious. It will have no effect if the
~/.bashrc.d
directory doesn’t exist. If it does exist, then each file in the directory will be sourced.Definitely non-standard, but I have something almost identical that I very consciously added and really simplifies a lot of things.
If you’re thinking it may be malicious, I think it’s innocuous.
Try cat’ing
/etc/skel/.bashrc
and see if the code in question in in there. My guess is it will be. When a new user’s home directory is created, it copies all the files from/etc/skel
into the newly-created home directory. So, that directory is basically a “new user home directory template.”The code you posted (is missing an
fi
at the end, but anyway) just looks like a utility for making it easier to organize your .bashrc into separate files rather than one big file. That’s a common technique for various configuration files that a lot of distros commonly do. And I personally find that technique nice.If you want to delete that code, it’s not going to hurt anything to remove it (unless someday you add a
~/.bashrc.d/
directory and some file in there “doesn’t work” and it confuses you why.)Also, what distro are you on?
The code did have a fi at the end, i am using fedora.
instead of curly brackets
if
statements are closed withfi
https://stackoverflow.com/questions/7010830/bash-whats-the-use-of-fi
i don’t know much bash, so anyone else who responds is probably more right, but since no one has responded, here’s my 2 cents: it appears to be a script to run all scripts in .bashrc.d. That would be similar to how some apps will let you separate a configuration file into multiple files in a directory conf.d. If there is no .bashrc.d, then it should be fine.