• 11 Posts
  • 47 Comments
Joined 9 months ago
cake
Cake day: January 9th, 2026

help-circle







  • I generally dislike using the set methods to change how the “language” works. So even if it covers my issue, I’m not using it. Its also not even said that these options couldn’t be changed, in fact I think in some cases it can be useful to change those options temporarily for certain effects like “pipefail” or the one that prints the executed lines. But not as a default or for critical commands that can run at any time the script exits (even on error). Maybe someone (even me) copy pastes this line in example.

    I always forget which of these set options do what, and next time when I write or read another script it could have different set of options. So I ignore those set options to change how the language is interpreted in Bash. Also if cd -> rm -rf -> rmdir solves it, why would I need to rewrite and change it to set -u -> rm -rf?


  • I know, good habit, check variable and so on. But mistakes happen, so what’s wrong with hardening the case? There are decades of studies and good practice for coding, yet the best programmers still do mistakes. And having a script that could potentially delete files on your system (even all subdirectories) is dangerous and should be handled with respect. I really don’t understand the opposition here.

    In terms of defensive programming, anything extra is added complexity that will only make an error more likely imo.

    I disagree here. Adding checks will help in catching those errors. Being not defensive about it will make it only more likely to make errors.

    no amount of trap logic is going to make a future programming error impossible.

    No amount of any programming will make it impossible to error out. That does not mean we shouldn’t try to make it as secure as we can think. Renaming the variable is putting the risk to another name, not really solving the issue.





  • Ah I absolutely misunderstood you. The PID trick is actually clever!

    But I would still not solely want to rely on a $TEMPDIR variable alone, without a fixed path like "${TEMPDIR}/tests". The reason is, I am not just concerned about the trap cleanup, but also the usage in the script. I want never use the variable in the script (after setting up trap) without a fixed path on it. In example the script will change filenames, eventually using glob patterns or do other stuff. If the script is faulty and changes the temporary variable, then it will at least do this under “tests” no matter what.

    The idea is neat though. I have to think about this, experiment and see if I end up using this.


  • You said you would just use scripts and use Ai, instead learning:

    use scripts and ai for everything and not learn anything

    If you are not interested into learning, how do you want program? My suggestion is to stop using any Ai tool, do not run others scripts until you understand what they are doing, and start learning. Maybe start with something like Python. There is a lot of material, tutorials and good documentation, even good help on YouTube, and a huge and I mean a huge community who is willing to help you. If you use Ai during this period, I don’t think you will learn much. In worst case, you get very bad habits and learn not to think critically.

    It would be good to know what you tried so far, what your interest is, if you have any ideas what you even want to program.


  • i dont program but if i did id proballey just use scripts and ai for everything and not learn anything

    If you used pre-written scripts or vibed your Ai output without learning, then you wouldn’t be programming.

    what do you do to motivate/stop yourself

    To motivate myself to program? First, if its not fun, then I don’t do it. Just a hobby for me. Sometimes I need some motivation, and that is a) I did it myself, can be proud of, b) it might help others, c) to solve a real issue I have myself, d) programming is fun, just for the sake of, e) the prospect of learning something and be able to solve it myself, and it could help solving real issues later when needed, it happened even for work. Just some stuff I can think off in my head.

    To stop to program? Well sometimes its time consuming without results, without finishing something. Even though I learn, it can feel waste of time. I could have done something else in this time. It’s just a hobby in most cases, so in most cases I compare programming to something like playing a game or watching a movie.

    What about you? If you are not interested into programming, why are you posting in programming?


  • Edit: Please ignore this reply here, and read the discussion following the answers. I absolutely misunderstood the above reply.


    mktemp --suffix=$$ does not really solve the issue I have. I don’t want to use ${TEMPDIR}, but have something hardcoded when using the variable, as in rm -rf "${TEMPDIR}" vs rm -rf "${TEMPDIR}/tests". Because I’m not worried about what mktemp gives me back, but about when the variable is used at later time. The content of the variable could be altered after its initial creation.

    In your trap assert that the content of var TEMPDIR ends with your same current PID or fail before you clean anything up.

    I quiet don’t understand this point here. The trap command will run in any case the script exits, be a crash or normal exit. If the variable or directory is invalid, then the cleanup will not be executed. But that is by design, because I do not want to cleanup something that is not working correctly. I rather leave it to be cleaned up automatically with next reboot.

    You could also assert TEMPDIR is prefixed with $TMP or /tmp for even more robustness.

    It has already a fixed suffix part with “/tests” in use. I’m not worried about the TEMPDIR content if mktemp created it correctly. I’m more worried about the variable being altered and invalid at later point in the script. I would rather leave mktemp create the directory where it thinks is the best place (mostly it is /tmp, but that is not guaranteed). So prefixing the variable content itself doesn’t really solve the trust issues I have here, as it is not the creation time that I’m worried about.


  • I actually have trash-cli installed and exclusively use it for deleting all trash directories available on my system. Because I experience some inconsistencies how applications handle trash directories, involving mounted external drives.

    Moving files instead deleting them, in case they are important files is a good advice. However in case of temporary created and deleted files for testing software, I think this goes a bit too far. But it could prevent data loss, in case something goes wrong and I delete the wrong directory (hopefully the trash directory does not get deleted too, due to recursive deletion). Overall, this is a good advice to have in mind, I just think it goes a bit too far in this use case.



  • My reply is rejecting (most of) your suggestions, with reasons off course. I am glad you bring them up, so we can talk about it.

    I would avoid set -e option, as I do not want he entire script to exit on error. So instead I can use the exit command when I really want to on specific errors. I rather would like to handle errors myself directly, maybe even not exiting, but displaying error code with $? in example.

    TEMPDIR=$(mktemp -d || echo "/invalid")

    If anything, it would make more sense to just exit the script with || exit. In fact that is what I’m doing in the script after the trap command by cd "${TEMPDIR}/tests" || exit, so the script never continues without a successful mktemp directory.

    TEMPDIR=${TEMPDIR:-"/invalid"}

    I always forget that Bash has default values for variables! mktemp actually makes sure it never returns an empty value. I’m not worried about what it returns, but that my script could change the value of $TEMPDIR by accident (in example to something empty). So assigning a default value after mktemp will never have a chance to get the default value at all.

    rm -rf ${TEMPDIR:-"/invalid"}

    This on the other hand I like a lot. Now I will not stop doing my other additional checks, but for good habit this can’t be wrong. Maybe instead a custom directory name with an unlikely name, what about pointing it to /dev/null? I actually like this idea and might incorporate it.


  • In case any process deletes files or the directory “$TEMPDIR” points to, the script should be covered, right? With cd -- "${TEMPDIR}/tests" it is guaranteed that the directory exists when running rm -rf command. And in case the “$TEMPDIR” variable is altered in any way (replaced or added home, ~ or any relative paths like …/…/…/home in example), at least having a hardcoded directory name with “tests” would make sure it never deletes anything under any circumstances that is not named “tests”.

    Unless symbolic links and other link files are involved and added to that directory.