• Skull giver@popplesburger.hilciferous.nl
    link
    fedilink
    English
    arrow-up
    3
    ·
    edit-2
    1 year ago

    About the name thing. Say I type:

    name="Gerald"
    wg genkey > ${name}.key
    

    Would my output then be a key generated by Wireguard and named “Gerald.key”?

    Yes, assuming the user you’re logged in as has access to write files in the current directory. In /etc/wireguard, that’s usually only possible if you’re logged in as root (or using an interactive sudo shell). Adding sudo to your prompt doesn’t help in that case, because sudo only works up to the redirection character (>).

    HOWEVER: if $name is supposed to contain John Cena you’ll need to use wg genkey > "$name" or wg genkey > "${name}". Otherwise, your private key will appear in a file named John, and the key contents would be followed by the word Cena! Spaces mess up the shell and for that quotes are very very useful.

    I think I’m mostly getting caught up in when the quotations are necessary and when they’re not.

    The exact rules differ per shell. The default on Ubuntu is bash, the rules for which I’ll add below. macOS and some other distros use zsh as a default, which is mostly bash compatible but includes some “most people probably mean to type this other command” style fixes.

    In general:

    • Double quotes are used for when a variable can contain spaces (like a file name)
    • Single quotes don’t expand variables; '$name' contains the literal string $name, not Eve Johnson
    • Omitting quotes will lead to variables being expanded in place. If the contents of the variable are a single word, that effectively works the same as if you use double quotes

    Suppose you have a directory with these files:

    • file
    • file with spaces
    • readme.txt

    Suppose you want to remove some files with this script, using rm to delete them:

    myFile="file with spaces"
    
    # first quote option
    rm $myFile
    # second quote option  
    rm '$myFile'
    # third quote option
    rm "$myFile"
    

    The first rm call will try to remove the files file, with, and spaces. It will delete the file named file and give you two errors for the missing other two files. It’s as if you typed rm file with spaces. rm takes a long list of files that need to be deleted, so it’ll think you want to delete three files!

    The second rm call will give you an error, telling you that there is no file called $myFile. It’s as if you typed rm '$myFile'. You can create a file with that name, though; touch \$myFile or touch '$myFile' will create a file starting with a dollar sign. Many tools forget that this is a possibility (just like * and ? are perfectly valid file names!) and scripting based tools can easily fail or crash or even get exploited by hackers if you place a file with special characters on the system!

    The third rm call will remove the file file with spaces and leave the rest alone. It’s as if you typed rm "file with spaces". It’ll remove the one file.

    Using ${variable} is a bit safer and clearer, because you indicate exactly where the name of the variable starts and where it ends. Quotes and such are still necessary, following the conventions above. This is useful when you try to create a string like backup_$date_morning_$user.bak; are you referring to a variable $date_morning_ or to a variable called $date? backup_${date}_morning_${user}.bak makes things a lot clearer!

    For my own sanity and to make sure I know what to expect, I generally use double quotes and preferably ${} style expansion ("$name") for strings I want variables to be expanded in and single quotes around strings I definitely do not want variables to mess with.

    • GiuseppeAndTheYeti@midwest.social
      link
      fedilink
      English
      arrow-up
      2
      ·
      1 year ago

      Wow! You absolutely know what you’re talking about! You did an amazing job clearing that up for me. I’ll save this comment in case I need to come back to it. Thank you!