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

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

    • melroy@kbin.melroy.org
      link
      fedilink
      arrow-up
      2
      ·
      31 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.

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

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

  • ohshit604@lemmy.halstead.host
    link
    fedilink
    arrow-up
    2
    ·
    2 hours 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
      1
      ·
      29 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.