Bash Tricks I Use

Treats for your Bash without bashing it too much!

Avoid bash if you can and use for example python. Reason is that the bash scripts cannot grow into a larger program, because debugging and crazy syntax.

But sometimes you will encounter bash.

Error Handling with Or Operator “||”

# I usually set these options.
# The -e option causes bash to exit immediately if any command exits with a non-zero status.
# The -x option prints every command before it is executed to simulate debugging.
# The -u option causes bash to exit immediately if any unset variable is used.

set -eux

echo "before";
  echo "inner";
  # This will cause exit from the function.
  echo "we will never get here";
} || echo failed

echo "continue here";

# This will cause exit.

echo "We will never get here :(";

Careful with the Star Expansion!

Remember .* expands also to .., which is outer directory!

Listen to The Output of an Already Running Process

The process needs not to have its output piped to a file or other pipe. You can access the output via the proc filesystem:

cat /proc/<pid>/fd/1.


echo "while true; do sleep 1;   echo here; done;" >;
nohup bash &
# prints: [1] 1020383
cat /proc/1020383/fd/1;

For stderr use 2 instead of 1:

cat /proc/<pid>/fd/2


  • 0: stdin
  • 1: stdout
  • 2: stderr

Avoid manual Exporting All Variables

Avoid manual exporting with allexport option.


set -o allexport

source .env
set +o allexport

Other Sources

  • TODO

Good luck!

Created on 09 Jun 2024. Updated on: 09 Jun 2024.
Thank you

About Vaclav Kosar How many days left in this quarter? Twitter Bullet Points to Copy & Paste Averaging Stopwatch Privacy Policy
Copyright © Vaclav Kosar. All rights reserved. Not investment, financial, medical, or any other advice. No guarantee of information accuracy.