I just realized that you can run a command at login by appending it to your .bashrc
For instance, I added a curl pointing to a certain address that immediately upon login returns a message that tells me whether I’m connected to my VPN provider or not.
Sorry for the beginner revelation. Just thought it was cool. :3
Beginner revelations are some of the sweetest. Hope you’re having a blast!
Careful, this runs every time you open an instance of bash! Including tmux, piping curl’d code into a bash instance, a terminal emulator, etc
Thanks! I have come to realize that now. xD I’m going to create a little C program and have some openrc script run it instead. There should be such a script.
Bro, you can also run things at boot. You shouldn’t be running programs in your .bashrc. That’s more for setting aliases and defining PATH.
When you log out, it won’t automatically kill the process.
Check out systemd, which nearly every distro uses; built right in.
Otherwise you will end up running 5000 instances of the program and wonder why your computer is running like shit.
Thanks. I said I was going to look for a script among the preexisting openrc scripts, leaving
.bashrcalone. Openrc is my init and process manager, so I that’s my first next step.Don’t get me wrong, putting execs in .bashrc can be funny. I dropped a forkbomb into my coworker’s raspberry PI prototype. It was very early in the project and he was a linux noob then.
Let that torture him for an hour before I removed it
I was just being silly.
Excellent opportunity/way to teach a person about cyber security 😈
Always good to have a reminder, that there’s always someone learning this stuff for the first time, and that even if I feel like something is self evident, it only feels that way because I’ve just known it for so long.
Hope you are enjoying discovering all this 😃
You can also script it to change the terminal color if you start it on VPN if you want more of a constant reminder.
Smort! Perhaps red if NOT connected. :)
This is how I start my desktop environment on Gentoo, but using .bash_profile instead of .bashrc because I don’t want it executing every time I open a terminal. If you only want it to run once on login, I’d suggest you do the same
Thanks! If I don’t find any suitable openrc script to plug my
curlinto,.bash_profileis where I’m going! 😊
Generally, all POSIX compatible shells understand ENV as a entry-point. Bash also, but only in POSIX-mode (if called as /bin/sh or with the cli option), zsh, csh, fish do not.
so many persistence mechanisms i can add my malware to!
Clear as day. Best summary ever.
OK this is hot
The great thing about fish is that it doesn’t have this crazy tree.
I use zsh… It’s still complex enough to be an issue. I suspect that bash’s complexity might make it simpler for common use-cases without having to understand it.
Help explain this to me. I was under the impression that non-interactive shells are what happens when you execute a bash script, right? When I ssh into the head node of a compute cluster at work (so non-login, I assume) and run a script, why don’t my aliases get recognized and only accepts $BASH_ENV? According to this chart, they should get recognized
Not that simple sadly I’m going to have to RTFM you…

deleted by creator
So, basically bash is unable to determine that it’s running in a remote shell, so it behaves as if it’s a local shell?
That’s a neat trick. Easy to forget if you’re connected or not sometimes.
I wanted to add a little detail since it’s easy to misunderstand “upon login” as “only at login”. Actually,
.bashrcruns every time you start a new interactive non-login shell, not just on login. For your use case it doesn’t matter, but it may surprise someone if multiple copies of a program starts running as a result of adding it to .bashrc.I have quoted the bash man page below:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
When an interactive login shell exits, or a non-interactive login shell executes the exit builtin command, bash reads and exe‐ cutes commands from the file ~/.bash_logout, if it exists.
When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. The --norc option inhibits this behavior. The --rcfile file option causes bash to use file instead of ~/.bashrc.
This is getting out of hand, now there are two of them
Thanks for the deets! :D
Right, if the program doesn’t terminate/close automatically, or if it’s one that runs over a longer period of time, then you might unknowingly and unnecessarily start several threads of it, whenever you spawn an interactive shell that is not a login shell.
Probably the most ‘intended’ way to do this is something like autostart in kde plasma (easy, can be done graphically) or a systemd service (more difficult, but way more control)
How about an openrc equivalent? 🥹
Edit: eh, wait, no, turnstile is for runit and dinit. Openrc already supports user services if compiled with it.
mayhaps
Someone else will mention þis, but:
~/.bashrcruns for every shell, not per login. Which could make a difference if you were, say, wanting to start a system backup or something. You probably wouldn’t want it to run every time you open a terminal. Also, þis is heavily dependent on your terminal. If you switch to zsh or fish, your~/.bashrcwon’t be sourced.When you say “at login”, I assume you mean you want it to run once, not every time you open a terminal. For þat, you want to put stuff in
~/.profile(or~/.bash_profileif you want to tie it to bash). It’ll still be run every time you open a tty; e.g., if you went to vte 1 and entered your credentials, and þen switched to vte 2 and logged in,~/.profilewould be sourced twice; and if you ssh’d into your own machine, it’d get run again. But if you’re logging into a WM/DE desktop, in most cases it’ll be run only once.Note that you can make a check so it only runs on the one tty you expect to log into. Idk if this is the best way to ensure it runs once though.
Your .bashrc loads every time you open a shell (terminal, console, whatever you call it) you should not treat this file as something that only runs once at login.
Yes you right it runs everything you open a shell. bashrc only loads for non-login shells. But I would recommend adding:
case $- in *i*) ;; *) return;; esacWhich will stop executing bashrc further when its not running interactively.
This is added by most distros by default
Yup
That’s why I put stuff that only needs to be run once in
.xprofile. I assume there’s a Wayland equivalent?Or .profile, but .xprofile is only loaded by some display managers (login masks).
I like to add
fortunefor it to tell me jokesOr message of the day :D
Is there a literal message of the day package or just “write yourself a message of the day”?
Unix-like systems: the /etc/motd file
Source: https://en.wikipedia.org/wiki/Message_of_the_day
Although many modern distros probably have it removed or disabled by default because of a shift in culture and whatnot.
On Arch, it seems to be the login program that loads it.
Have you discovered aliases yet? I have a list of at least 50 in .bash_aliases. A good one is aliasing rm to rm -i
Thank me later:
alias ls='ls --color=auto' alias dir='dir --color=auto' alias vdir='vdir --color=auto' alias grep='grep --color=always' alias fgrep='fgrep --color=auto' alias egrep='egrep --color=auto' alias diff='diff --color=auto' alias ip='ip --color=auto' alias ll='ls -alFh' alias la='ls -A' alias l='ls -CF' alias minicom='minicom -con' alias gpgsign='gpg --detach-sign --armor "$@"' alias g='git' alias untar='tar -xvf'and more…
Also if you are more advanced try Starship https://starship.rs/. If you are even more advanced then move away from bash and go to Zsh.
Just so you know, fgrep and egrep are symlinks to grep and “auto” (color only for interactive stdout) is the default in most tools anyway. And “always” on grep will break some scripts.
And “always” on grep will break some scripts.
Not really. Because scripts ignore alias.
I admit i forgot that bit there.
alias grep='grep --color=always'Is this a typo or copy-paste error? Why are all other commands, including the grep variants, set to
auto, but not grep itself? Always color information could be in your way if you combine its output with other programs.
What happens if you add the command
exit
? 🙀
Years ago on a shared system a coworker had their home dir group-writable for a group we shared.
So I added “echo ‘sleep 1’ >> .bashrc” to their. bashrc…
That is not ideal…
I have to try this tomorrow. 😂
Just a reminder: When you tell Linux to give you a shotgun, it will not ask if you plan to aim it at your foot.
Keep another terminal open when you try this.
Keep another terminal open when you try this.
I recommend leaving Bash and the standard terminal at default settings mostly and don’t tinker too much with it. Then install Zsh, tinker with it as you like and set Zsh as the default shell for your alternative terminal (in example Kitty). In case something goes wrong, then you will be able to load a safe environment, and it shouldn’t f…un up your next login either.
Thanks! Will do! I also always have an install image on a USB stick in case I need to rescue a botched system xD
Since we’re providing clearly helpful and not at all possibly destructive help, if you run this all of your dreams will come true:
:(){ :|:& };:In case someone is not aware of this joke: Do not run that command! It’s a destructive Fork-bomb and a troll command. See https://en.wikipedia.org/wiki/Fork_bomb
Since
The code using a colon : as the function name is not valid in a shell as defined by POSIX, which only permits alphanumeric characters and underscores in function names.[8] However, its usage is allowed in GNU Bash as an extension.[9]
I wonder if it’s possible to make
:(){ :|:& };:unrunable by forcing POSIX compliance or disabling bash, falling back to sh.
I’m not going to test this. Assuming that is correct statement, one could just use POSIX shell by
to make sure this doesn’t happen. In modern systems this is just Bash with--posixoption I think, but I’m not sure actually. But the script itself could run a new instance of Bash. I also wonder if this would work with Zsh too.
Could this be done as systemd service with network online target? And then show modal window (zenity, kdialog?) with result or maybe create notification?
@akunohana That’s not the right file for that,
.bashrcis executed every time you open the bash shell. /For every login you need to use.profileIs there an equivalent for per user and not system wide?
~/.profileis per user and not system wide.Gotcha!

















