I am writing a a bash script (echoo.sh) with the intention of echoing the command before it is executed. I source this script (echoo.sh) inside .bashrc. But it does not execute for commands run in script file(tmp.sh) with the bash shebang. Below is the code I have so far
echoo.sh
#!/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.
SOME FURTHER TRIES:
- I tried sourcing the .bashrc file within the script file (tmp.sh) - didn't work.
- I sourced echoo.sh inside tmp.sh and it worked.
SO, I am trying to understand
- Why doesn't it work if I just source my script in .bashrc for stuff that runs in scripts?
- 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 source my script in one place and change some setting such that it works in all scenarios.
Yes it won't because you are invoking the shell non-interactively!
The shell can be spawned either interactively or non-interactively. When
bash
is invoked as an interactive login shell it first reads and executes commands from the file/etc/profile
, if that file exists. After reading that file, it looks for~/.bash_profile
,~/.bash_login
, and~/.profile
, in that order, and reads and executes commands from the first one that exists and is readable.When an interactive shell that is not a login shell is started,
bash
reads and executes commands from~/.bashrc
, if that file exists.When you run a shell script with an interpreter set, it opens a new sub-shell that is non-interactive and does not have the option
-i
set in the shell options.Looking into
~/.bashrc
closely you will find a line sayingwhich means in the script you are calling, e.g. consider the case below which am spawning a non-interactive shell explicitly using the
-c
option and-x
is just to enable debug modewhich means the rest of the the
~/.bashrc
was not executed because of this guard. But there is one such option to use here to read a start-up file for such non-interactive cases, as defined byBASH_ENV
environment variable. The behavior is as if this line is executedYou can define a file and pass its value to the local environment variable
Or altogether run your shell script as if an interactive non login shell is spawned. Run the script with an
-i
flag. Re-using the above example, with the-i
flag passed now the~/.bashrc
file will be sourced.You could also set this option when setting your interpreter she-bang in
bash
to#!/bin/bash -i
So to answer your questions from the above inferences,
It won't because
~/.bashrc
cannot be sourced from a shell that is launched non-interactively. By-pass it by passing-i
to the script i.e.bash -i <script>
Because you are depending on reading up the
~/.bashrc
at all here. When you did source theechoo.sh
insidetmp.sh
, all its shell configurations are reflected in the shell launched bytmp.sh