1

Is there a way to persist file redirection commands between exec calls?

For example, there are two bash scripts. In script 1, I am setting up redirection of stdout and stderr to file. Then exec-ing script2.

script1

exec &> >(tee "$logs_file")
echo "something"
exec "./script2.sh"

script2

echo "something else

Inside log file only the echo commands from script 1 appear but not script 2.

something

Is there a way to persist file redirection commands between exec calls?

1
  • You've accepted my answer (thank you). Have you worked out why it wasn't working for you originally? Dec 4, 2019 at 6:49

1 Answer 1

0

Fixing a couple of minor errors in your example, I don't see the problem you describe

# Set up the scenario
mkdir /tmp/555270
cd /tmp/555270

cat <<'EOF' >script1
#!/bin/bash
logs_file=logs_file
exec &> >(tee "$logs_file")
echo "something"
exec "./script2"
EOF

cat <<'EOF' >script2
#!/bin/bash
echo "something else"
EOF

chmod a+x script?
rm -f logs_file

# Run the script
./script1
something
something else

# Examine the log file
cat logs_file
something
something else

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .