• @[email protected]
      link
      fedilink
      English
      63 days ago

      pushd and popd are also pretty neat in that they allow you to change directories using a stack; particularly useful as part of that is that using pushd without any arguments will pop the directory on top of the stack and move you there, while putting your previous directory on top of the stack. When you’re working across directories where you need to move around within each directory, it can be really handy.

    • @apex32
      link
      English
      13 days ago

      Nice!

      Also cd without any parameters takes you to your home directory.

    • @EtherWhack
      link
      English
      03 days ago

      ‘cd …’ goes up a level

  • @waigl
    link
    English
    16
    edit-2
    3 days ago

    Bottom left example will leave you with a lot pictures ending on “.png.jpg”.

      • Great Blue Heron
        link
        fedilink
        English
        9
        edit-2
        3 days ago

        convert $i ${i%.*}.jpg

        NB: I’m no guru - I just googled “remove file extension in bash”.

        • @qqq
          link
          English
          43 days ago

          Should also definitely be quoting both arguments

  • @[email protected]
    link
    fedilink
    English
    12
    edit-2
    3 days ago

    The most useful bash trick for me is to save what I learn in demonstration scripts so I can $cat it later for a syntax reminder.

    ex :
    file named : ./follow_jellyfin_system_log

    contents :
    #!/bin/bash
    journalclt -fe -u jellyfin

    (some time later) … wtf args do i pass to journalctl again…

    $ls ./script_parent_directory

    (list of actions written in human language appear as file names)

    $cat ./follow_jellyfin_system_log

    me : “oh duh!”

    rinse and repeat for every command or shortcut you learn, it forces you to convert the command into a short “noun_verb” format which to me is infinitely more understandable.


    that’s my workflow. then the meta is deciding how to store your scripts so you don’t end up with a polluted home dir. godspeed fellow bashers.

  • ElsieM
    link
    fedilink
    English
    133 days ago

    Remember for $() to wrap it in quotes to prevent word splitting!!!

  • KSP Atlas
    link
    fedilink
    English
    63 days ago

    Another cool bash trick: $! gets all the arguments of the previous command, but not the command itself

  • @eyeon
    link
    English
    63 days ago

    another useful one is <(cmd here)

    It’s like $(cmd here) but instead of returning the output of the command as a string passed into the arguments, it makes a new file descriptor for the output of the command and returns the path to it, for use in commands that want a file as an argument.

    So for example if you want to use diff to compare your local passwd file to a remote one…

    diff -u <(ssh host1 cat /etc/passwd) /etc/passwd

    Admittedly I can’t think of many things I’ve used this for outside of diff and alternatives to diff…but I’ve used it there a lot, like comparing the results of running two slightly different queries.

    • v_krishna
      link
      fedilink
      English
      43 days ago

      I use this one for comm a lot. E.g.,

      comm -12 <(sort some_file) <(grep foo some_other_file | sort)

  • @aberrate_junior_beatnik
    link
    English
    73 days ago

    The most useful bash command of all: sudo apt-get -y install zsh; chsh -s zsh; exec zsh ;)

    I kid, I kid! I actually like bash a lot. I really prefer it for scripting. Arrays are extremely useful but not specified in posix. Bash is pretty ubiquitous though.

    I’m also curious if there are any bash partisans who prefer it over zsh and for what reasons.

    • @[email protected]OP
      link
      fedilink
      English
      73 days ago

      I actually prefer fish, but I want to learn more bash because is is ubiquitous and a lot of bash stuff also works in fish.

      • @[email protected]
        link
        fedilink
        English
        33 days ago

        You may also like NuShell. It’s been designed from the ground up to be an intuitive and convenient shell language.

      • @aberrate_junior_beatnik
        link
        English
        23 days ago

        Fish is great! I used it for a while a long time ago but I ended up stopping because a few tools expected your shell to be set to something posix compatible. I wonder if the support is better now, I should give it a shot

    • @ziggurat
      link
      English
      33 days ago

      I’m a serious bash scripter, I mean I do it both at work and as a hobby. And zsh arrays are better. Like you can use negative indecees and stuff. I have only written shell functions in zsh (my primary shell), and not thousands of lines of scripts like I have for bash. But if zsh had as good documentation and language servers I’d probably do many scripts in zsh instead. But I can’t say for certain, as I am not as sure about performance in some situations and stuff.

      Why don’t I use posix shell if I care about performance? Because they are only faster in very simple scripts,

  • Programmer Belch
    link
    fedilink
    English
    3
    edit-2
    3 days ago

    I am not sure what the default keybinding is for this but I always create a .inputrc file for searching forward and backward in the history without opening the C-r menu:

    "C-p": history-search-backward
    "C-n": history-search-forward