• Übercomplicated@lemmy.ml
    link
    fedilink
    English
    arrow-up
    1
    ·
    2 hours ago

    None of these are accurate. The only shell trick that genuinely saves your sanity is fish. (/jk)

    Also, these are genuinely very basic tips. It’s much more useful to learn about tmux, plugins (e.g., atuin), and useful commands like sed, grep, fd, awk, etc. Pretty much everything this article mentions becomes second nature in fish, leaving you to focus on more important things. Like learning neovim or emacs!

  • amateurcrastinator@lemmy.world
    link
    fedilink
    English
    arrow-up
    2
    ·
    8 hours ago

    On a new rocky 10 install Ctrl + R doesn’t do much. I can’t find commands I know I typed. I can’t be bothered to find out why

  • Undearius@lemmy.ca
    link
    fedilink
    English
    arrow-up
    29
    ·
    1 day ago

    I’ve seen a few of these “shell tricks” articles. This one I actually learned quite a few, which I will promptly forget when I actually need to use them.

    • Ŝan • 𐑖ƨɤ@piefed.zip
      link
      fedilink
      English
      arrow-up
      8
      arrow-down
      19
      ·
      1 day ago

      I keep a text file full of þese sorts of þings. Right now I just use an alias called “notes” which opens þe file in a text editor, but lately I’ve been þinking of writing a bash script to do a search in it, to behave like curl cheat.sh/restic, but tag-based. Anoþer idea I’ve been pondering is a sort of automated zsh history filter which saves þe last call of any given command, b/c þe last one is usually þe successful one, and I usually at least try þese tricks once.

      Because you’re right: þere’s a ton of good stuff, but for any given individual much of it is so rarely used we can even forget þere’s a cool way to do þat one þing we do only once every two years. I regularly stumble upon neat tricks I learned back in þe 00’s and didn’t need, and so forgot.

      • toynbee@piefed.social
        link
        fedilink
        English
        arrow-up
        2
        ·
        11 hours ago

        Wow, haven’t seen your thorns in a while.

        I liked vimwiki for this, except that it set expandtab and I could never find where.

    • toynbee@piefed.social
      link
      fedilink
      English
      arrow-up
      5
      ·
      11 hours ago

      Pretty much the one and only good thing about work forcing us to switch from Linux laptops to Macbooks.

      • MummifiedClient5000@feddit.dk
        link
        fedilink
        English
        arrow-up
        5
        ·
        10 hours ago

        The mixed used of the command key and CTRL on Macs has tricked me to pressing command+w in a terminal window quite a few times.

      • LiveLM@lemmy.zip
        link
        fedilink
        English
        arrow-up
        5
        ·
        11 hours ago

        I hated the Command + C and Command + V for Copy / Paste until I realized it meant not clobbering ^C in the terminal. Instantly loved it.

        • toynbee@piefed.social
          link
          fedilink
          English
          arrow-up
          4
          ·
          10 hours ago

          The biggest issue I have with it is that I have Linux on all my personal systems and OSX on my work laptop and sometimes switch rapidly between them.

          My fingers don’t seem to adapt as quickly, though, and I often press the wrong combination between them.

      • theherk@lemmy.world
        link
        fedilink
        English
        arrow-up
        5
        ·
        1 day ago

        They’re common in clouds like Azure, AWS, etc. Life is better with ssh, but sometimes these are useful for bastions.

      • MummifiedClient5000@feddit.dk
        link
        fedilink
        English
        arrow-up
        2
        ·
        1 day ago

        I prefer an actual client, especially because I press CTRL+w before my brain sets in, but there are plenty of examples. My latest encounter was Rancher, a k8s management thing.

        But a client running in a browser window does not imply any remotely exploitable vulnerabilities.

  • Badabinski@kbin.earth
    link
    fedilink
    arrow-up
    9
    ·
    24 hours ago

    set -e: Exit on error. Very useful, but notoriously weird with edge cases (especially inside conditionals like if statements, while loops, and pipelines). Don’t rely on it blindly as it can create false confidence. (Pro-tip: consider set -euo pipefail for a more robust safety net, but learn its caveats first.)

    while I appreciate that the author mentions how weird this is, nobody is going to learn all the caveats correctly. Don’t use set -e. Don’t use set -e. Don’t use set -e. It’s a shit ass broken ass fucked feature that half of nobody understands well. Here’s a great wiki page explaining why it’s trash: https://mywiki.wooledge.org/BashFAQ/105

    People like Go, and Go requires you to manually and stupidly handle every possible error case. Why not do the same for shell? It’s really quite easy:

    #!/usr/bin/env bash
    echoerr() { echo "$@" 1>&2; }
    
    die() {
      message="$1"; shift
      exit_code="${1:-1}"
      echoerr "$message"
      exit "$exit_code"
    }
    
    temp_dir="$HOME/tmp"
    mkdir -p "$temp_dir" || die "Failed to make persistent temporary dir $temp_dir"
    lc_dir="$(mktemp -d -p "$temp_dir")" || die "Failed to make target dir in $temp_dir"
    

    Look at that, descriptive error messages! And it doesn’t depend on a shell feature that is inconsistent between versions with no good documentation about all of the fucked up caveats.

    • Coriza@lemmy.world
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      12 hours ago

      You know… I was about to reply with "I use set -e and I love it, but them I read the link and it gave me flashbacks. In a previous work at some points I programmed way more in bash than the languages I was officially hired to program into, and I run in some many of this edge cases, I think almost all of the ones mentioned in the link, including doing the workarounds mentioned. two that standed out to me was local var=$(fail) and if f(). Side note, I remember finding a bug in declare (I don’t remember exactly, but one of the flags, maybe -l to make a local variable was not working) and was só excited to fill a bug report but then I saw that it had already fixed in a newer bash release.

      Anyway, In the end if I recall correctly I never settled in a one fixed approach, I distinctly remember adding set -eu to a bunch of scripts but also remember having to set +e in some cases like when sourcing other scripts and also adding the suggested foo || die combo a bunch"

      I think in the end my approach was the same as rking’s at the end of the linked text, I used it but not relied on it. And I guess using too much bash made me paranoid and I heavily tested every line for all sorts of error cases. I think set -e is good when debugging when the thing is not working, especially because bash also suffers to lack a good debug and tracing environment, leaving you to set a bunch of stuff just to have a trace of your script and see what it is doing.

      • toynbee@piefed.social
        link
        fedilink
        English
        arrow-up
        2
        ·
        11 hours ago

        I remember a while ago - when, like in your anecdote, I mostly coded in bash - I had a dream that I found out people were invoking my scripts in a manner that essentially overrode settings that might (or, in my case, might not) have been set at the beginning of the script.

        This never (AFAIK) happened in waking hours, but I was very offended in the dream.

  • Voytrekk@sopuli.xyz
    link
    fedilink
    English
    arrow-up
    6
    ·
    22 hours ago

    Great article. I have leanred a lot of these tricks and shortcuts over the years yet I still learned quite a few things that would be useful.

    Particularly that you can paste what you cleared with ctrl+U. No more opening a second tab or creating an additional ssh connection.

  • CallMeAl (like Alan)@piefed.zip
    link
    fedilink
    English
    arrow-up
    12
    ·
    1 day ago

    Great article but this shouldn’t be called “Tricks” it should be called Shell Basics. I’m old enough to remember taking an Intro To Unix course and there was an entire day on the shell where these types of commands were presented as essential learning.

    • toynbee@piefed.social
      link
      fedilink
      English
      arrow-up
      2
      ·
      11 hours ago

      A fact that I like to share from my personal history: I took four years to graduate from a two year college because I was taking every computer class they offered … Except that I skipped “intro to Unix” because when was I ever going to use that?

      My entire career has been largely based on knowing how to use Linux.

  • MonkderVierte@lemmy.zip
    link
    fedilink
    English
    arrow-up
    8
    arrow-down
    3
    ·
    edit-2
    1 day ago

    Please make the site respect my color setting by default.

    It’s a sunny spring day.