I am writing a a bash script echoo.sh with the intention of echoing commands before they are executed. I source the script inside .bashrc. But it does not affect other bash scripts that I run. Below is the code I have so far:


#!/usr/bin/env bash
shopt -s extdebug
get_hacked () {
    [ -n "$COMP_LINE" ] && return  # not needed for completion
    [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # not needed for prompt
    local this_command=$BASH_COMMAND;
    echo $this_command;
trap 'get_hacked' DEBUG

When I open a shell and run any command - It works. But for stuff in a script file it doesn't work.


  1. I tried sourcing the .bashrc file within the test script - it didn't work.
  2. I sourced echoo.sh inside of the test script and it worked.

So, I am trying to understand

  1. Why doesn't it work if I just source my script in .bashrc for stuff that runs in scripts?
  2. Why doesn't further try #1 work when #2 does.

And, finally, what can I do such that I don't have to source echoo.sh in all script files for this to work. Can I source my script in one place and change some setting such that it works in all scenarios.

  • ]shopt? With the bracket? – ilkkachu Feb 6 '18 at 19:49

I tested all your tries, and it worked on archlinux, bash version 4.4.12. But I had to open a new interactive shell, so the new ~/.bashrc could be sourced.

For non-interactive shells you could also try to setup an ~/.bashenvrc and set export BASH_ENV=$(realpath ~/.bashenvrc) in ~/.bashrc.

Now the script ~/bashenvrc will be sourced every time you start an non-interactive shell.

I tested this also with daemonize 1.7.7-1


  • Hey.. Thanks for trying it out.. I know it works for interactive shells. I am trying to get to work on non interactive shells as well – Sam Thomas Feb 7 '18 at 0:12
  • Hey.. no problem. I will need this too. So I edited my answer, please try my suggestion. – Dominik Kummer Feb 7 '18 at 9:22
  • Can you have a look at this ?? – Sam Thomas Feb 7 '18 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.