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

  • folekaule@lemmy.world
    link
    fedilink
    arrow-up
    21
    ·
    4 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.

    • printf("%s", name);@piefed.blahaj.zoneOP
      link
      fedilink
      English
      arrow-up
      6
      ·
      3 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.