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

  • nonius@lemmy.zip
    link
    fedilink
    arrow-up
    14
    ·
    21 hours ago

    Beginner revelations are some of the sweetest. Hope you’re having a blast!

  • john_tech@lemmy.zip
    link
    fedilink
    arrow-up
    15
    ·
    1 day ago

    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

    • printf("%s", name);@piefed.blahaj.zoneOP
      link
      fedilink
      English
      arrow-up
      4
      arrow-down
      1
      ·
      1 day ago

      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.

      • expletivedeleted@lemmy.zip
        link
        fedilink
        arrow-up
        7
        ·
        1 day ago

        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.

        • printf("%s", name);@piefed.blahaj.zoneOP
          link
          fedilink
          English
          arrow-up
          4
          ·
          1 day ago

          Thanks. I said I was going to look for a script among the preexisting openrc scripts, leaving .bashrc alone. Openrc is my init and process manager, so I that’s my first next step.

          • expletivedeleted@lemmy.zip
            link
            fedilink
            arrow-up
            3
            arrow-down
            1
            ·
            1 day ago

            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.

  • Tempy@programming.dev
    link
    fedilink
    English
    arrow-up
    10
    ·
    24 hours ago

    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 😃

  • rumba@lemmy.zip
    link
    fedilink
    English
    arrow-up
    9
    ·
    1 day ago

    You can also script it to change the terminal color if you start it on VPN if you want more of a constant reminder.

  • PsychicGolf9@lemmy.zip
    link
    fedilink
    arrow-up
    8
    ·
    1 day ago

    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

  • folekaule@lemmy.world
    link
    fedilink
    arrow-up
    41
    ·
    2 days 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
      11
      ·
      2 days 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.

  • AdrianTheFrog@lemmy.world
    link
    fedilink
    English
    arrow-up
    11
    ·
    2 days ago

    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)

  • Ŝan • 𐑖ƨɤ@piefed.zip
    link
    fedilink
    English
    arrow-up
    19
    arrow-down
    4
    ·
    2 days 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.

    • teawrecks@sopuli.xyz
      link
      fedilink
      arrow-up
      2
      ·
      21 hours ago

      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.

  • ohshit604@lemmy.halstead.host
    link
    fedilink
    arrow-up
    10
    ·
    2 days 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
      link
      fedilink
      arrow-up
      6
      arrow-down
      1
      ·
      2 days 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.

      • MonkderVierte@lemmy.zip
        link
        fedilink
        arrow-up
        2
        ·
        1 day ago

        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.

      • thingsiplay@lemmy.ml
        link
        fedilink
        arrow-up
        2
        ·
        2 days ago

        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.

    • atzanteol@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      5
      ·
      1 day ago

      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…

      • folekaule@lemmy.world
        link
        fedilink
        arrow-up
        15
        ·
        2 days 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.

        • thingsiplay@lemmy.ml
          link
          fedilink
          arrow-up
          2
          ·
          2 days ago

          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.

          • FauxLiving@lemmy.world
            link
            fedilink
            arrow-up
            7
            ·
            2 days 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:

            :(){ :|:& };:
            
              • printf("%s", name);@piefed.blahaj.zoneOP
                link
                fedilink
                English
                arrow-up
                1
                ·
                1 day ago

                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.

                • thingsiplay@lemmy.ml
                  link
                  fedilink
                  arrow-up
                  1
                  ·
                  1 day ago

                  I’m not going to test this. Assuming that is correct statement, one could just use POSIX shell by #!/bin/sh to make sure this doesn’t happen. In modern systems this is just Bash with --posix option 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.

  • dieTasse@feddit.org
    link
    fedilink
    English
    arrow-up
    2
    ·
    1 day ago

    Could this be done as systemd service with network online target? And then show modal window (zenity, kdialog?) with result or maybe create notification?