• 0 Posts
  • 7 Comments
Joined 11 months ago
cake
Cake day: February 9th, 2025

help-circle




  • I can’t tell you how many times I’ve accidentally pasted random private stuff from that goddamn middle click into WEB PAGES! Things that can read whatever text you type without having to explicitly submit anything. It’s a horrible thing for a new user to discover by accident. It’s such an unexpected feature to new users, and no one gets told about it, ever. You simply discover it by accident.

    This is a good change, not having it on by default.

    To all the haters of this idea, god forbid we make Linux less weird by default for people migrating from Windows.

    All that said, I have learned to love select-to-copy and middle-click paste. Especially in the terminal.


  • for loops

    Your code executes ls and records the results in a variable. The result is some text, a string of characters. (We call them “strings” and i is now a string variable.) Among the characters in a string variable might be spaces, tabs, or new line characters. I mention this because the special variable IFS is used by for loops, and it contains exactly one space, tab, and new line by default.

    When you call for with a string as the input, it splits the string into units by splitting on each character in IFS. That is, it splits the big string into individual parts by splitting at each space, tab and new line. So this creates an array which is what is looped over. Each word in turn is assigned to your looping variable and then the code after the do is executed once per word.

    (“word” has a sort of a special meaning here. When I say word, I mostly just mean a string that has no spaces in it. When you read text in English, there are words. They’re strings of characters separated by spaces. But words can also be separated by tabs, new lines, commas, semicolons, or whatever, but not by default when using for! You have to modify IFS to add those characters if you want them to be considered word separators.)

    So, if any of the file system entries returned by ls have spaces in them, your loop is going to create more outputs than there are file system entries in the current directory.

    For example:

    file one.txt
    file two.txt
    my photo.jpg
    notes (final).md
    a b c d.txt
    

    That would cause like 12 loops and 12 outputs in your code despite there only being five files.

    If you instead overwrite IFS before running your loop, and only assign a single new line to the variable, then your loop will only be over the actual lines of the input text. Like this:

    IFS=$'\n'

    and then use your exact code above. Using my example of five files, this code will now only produce 5 outputs, not 12.

    You can assign whatever characters you want to IFS.

    (I have not tested any of this code, or examples.)

    Variable names

    The loop variable name i is just an identifier. Any valid variable name would work except you can’t use the reserved names like $1, $2 or any keywords as names. Also, there’s no way to escape an identifier. They are just literal names.

    You also don’t want to use any built-in variable names or else you’ll overwrite their values for the duration of the current session. Bash will happily let you use them as your looping variable, but the rest of your code might have undesirable results. Variable names like IFS, for instance. :D