37
Run command at login (piefed.blahaj.zone)

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

top 22 comments
sorted by: hot top new old
[-] folekaule@lemmy.world 15 points 2 hours ago

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, .bashrc runs 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.

[-] akunohana@piefed.blahaj.zone 5 points 2 hours ago

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.

[-] maxihuenu@lemmy.ml 1 points 50 minutes ago

can you elaborate more? im more begginer than you (lol) and i dont fully understand what it means. Is it like cron?

[-] melroy@kbin.melroy.org 1 points 30 minutes ago

No its not like cron. The moment you open a terminal, the .bashrc script is automatically executed (for NON-login shells), but only when you start a terminal using bash. And it will get executed everything you open again a new shell/terminal.

Using source, anyhow. It is true you can add anything you like to your .bashrc file.. Often at the top you might have:

case $- in
    *i*) ;;
      *) return;;
esac

This will return directly (stops) the bashrc script, since you do not want to this this script most likely when you are not running interactively.

Its located in your home directory so: ~/.bashrc.

Finally, .bashrc is often used for setting aliases, or extending your PATH, sourcing other files actually otherwise the bashrc file becomes too large (like bash auto complete). Normally you don't want to execute scripts that take too long to execute.

I do recommend looking in other improvements to your shell like https://starship.rs/

[-] Sxan@piefed.zip 4 points 1 hour ago

Someone else will mention þis, but: ~/.bashrc runs 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 ~/.bashrc won'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_profile if 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, ~/.profile would 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.

[-] arran4@aussie.zone 8 points 1 hour ago
[-] akunohana@piefed.blahaj.zone 3 points 1 hour ago

Clear as day. Best summary ever.

[-] ohshit604@lemmy.halstead.host 2 points 1 hour ago* (last edited 1 hour ago)

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.

[-] melroy@kbin.melroy.org 1 points 25 minutes ago

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;;
esac

Which will stop executing bashrc further when its not running interactively.

[-] nonentity@sh.itjust.works 1 points 2 hours ago

Add it to your prompt, will test and indicate after every command completes.

[-] akunohana@piefed.blahaj.zone 1 points 1 hour ago
[-] nonentity@sh.itjust.works 1 points 1 hour ago

Look into the $PS1 and $PS2 environment variables.

[-] akunohana@piefed.blahaj.zone 1 points 1 hour ago
[-] nettie@lemmy.world 3 points 2 hours ago

What happens if you add the command

exit

? 🙀

[-] melroy@kbin.melroy.org 1 points 27 minutes ago

That is not ideal..

[-] akunohana@piefed.blahaj.zone 2 points 2 hours ago

I have to try this tomorrow. 😂

[-] folekaule@lemmy.world 5 points 1 hour ago

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.

[-] akunohana@piefed.blahaj.zone 2 points 1 hour ago

Thanks! Will do! I also always have an install image on a USB stick in case I need to rescue a botched system xD

[-] FauxLiving@lemmy.world 1 points 41 minutes ago

Since we're providing clearly helpful and not at all possibly destructive help, if you run this all of your dreams will come true:

:(){ :|:& };:
[-] luthis 2 points 53 minutes ago

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

[-] melroy@kbin.melroy.org 2 points 28 minutes ago

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.

this post was submitted on 04 Dec 2026
37 points (97.4% liked)

Linux

69006 readers
1179 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 7 years ago
MODERATORS