Artificial intelligent assistant

try/finally with bash shell I have these three lines: export bunion_uds_file="$bunion_socks/$(uuidgen).sock"; "$cmd" "$@" | bunion rm -f "$bunion_uds_file" I need to make sure the last line always executes..I could do this: export bunion_uds_file="$bunion_socks/$(uuidgen).sock"; ( set +e "$cmd" "$@" | bunion rm -f "$bunion_uds_file" ) or maybe like this: export bunion_uds_file="$bunion_socks/$(uuidgen).sock"; "$cmd" "$@" | bunion && rm -f "$bunion_uds_file" || rm -f "$bunion_uds_file" I assume creating the subshell and using set +e is slightly less performant etc.

You could set a trap:


#!/bin/bash

export bunion_uds_file="$bunion_socks/$(uuidgen).sock"
trap 'rm -f "$bunion_uds_file"' EXIT

"$cmd" "$@" | bunion


This would make the `rm -f` command run whenever the shell session terminates, except for when terminating by the `KILL` signal.

As mosvy points out in comments, if this is a socket that needs to be cleaned up before use, it would be easier to remove it before recreating and using it:


#!/bin/bash

export bunion_uds_file="$bunion_socks/$(uuidgen).sock"
rm -f "$bunion_uds_file" || exit 1

"$cmd" "$@" | bunion

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 2ab7507ee620d9a671e2eb1e1cf0cdef