filter:   bash ×
Sunday, September 6th, 2026 bash techniques utils • 208w

You just need a simple temp file to do some things, you find mktemp, perfect, you do your thing everything works. But wait... is the file deleted magically or do I have to delete the file. Of course you do have to.

Then you send the rest of your day to basically try to invent defer: you add rm on all exits, then you add a trap, then you add a second temp file so you have to add a second trap, of course the second trap overwrites the first one, then...

But this not my job, I didn't create the file, why should I delete?

So let make a script that create the file, echo the name, and in the background wait for the caller process to end (pidwait $PPID) and then delete the file:

name="$(mktemp ...)"
echo "$name"
(
  pidwait -p "$PPID"
  rm -f "$name"
) &>/dev/null &

And then you call it in a script and when the script end the file is removed like magic!



done_

Tuesday, January 27th, 2026 bash utils • 91w

Are you bored of typing ls all the time in your terminal. Wouldn't be greate if you could just hit a shortcut? You are in luck, because you just can. No addon, no nothing, just with the built-in bind:

bind '"\el":"ls -thor\n"'

Here you just bind Alt-L to run ls -thor.



done_

Sunday, February 7th, 2021 bash code easy utils • 182w

Ok, lets create a single line of bash that will handle the -h option and print something.

First, what to print? I usually add some comments on the start of the file, so lets print that. Example script:

#!/bin/bash
#
#  This is the doc of the script
#

the line that we will add

code of script

Lets go step by step. We need to print the file it self:

cat "$0"

Skip the first line (lets switch to sed):

sed -n '2,$ p' "$0"

Go until you find the first empty line:

sed -n '2,/^$/ p' "$0"

Remove the # fro the start of the line:

sed '2,/^$/ s/#// p' "$0"

Finally, we check if the -h is the first argument, print the thing and exit:

[ "$1" = '-h' ] && sed -n '2,/^$/ s/#// p' "$0" && exit



done_