You can use a feature of bash
that allows you to intercept user input and act on it, overriding any usual command execution.
Add the following to your bash
startup script:
shopt -s extdebug
function auto_source_names {
local CMD=$BASH_COMMAND
if [[ -f "$CMD" ]] && [[ ! -x "$CMD" ]] ; then
source "$CMD"
return 1
else
return 0
fi
}
trap 'auto_source_names' DEBUG
This causes the function to be called for every command executed. If it's the name of a file that isn't executable (otherwise we'd block legitimate ./foo.sh
calls as well), it is sourced and no command executed. Be aware of files named ls
or rm
in the same directory. You could add additional safeguards, e.g. requiring .sh
file extension or file
telling you the name is a text file.
Example:
Create foo.sh
:
FOO=bar
Then:
$ echo $FOO
$ foo.sh
$ echo $FOO
bar
Do you just want to execute the commands in the script? – terdon – 2012-12-05T16:01:25.943
no, I want them to act as source shell commands, without using the source keyword in the execution call – Andrew-George Hondrari – 2012-12-05T16:03:46.830
2Could you post your script (or the relevant parts of it) so we can better understand what you are trying to do please? Also, please explain why you do not want to use source. – terdon – 2012-12-05T16:12:21.213
2If it is just the amount of typing you want to avoid, note that the syntax
. file
is equivalent tosource file
Actually,source
is a "bashism" (Bash specific) and the dot operator is "standard" (POSIX). – Daniel Andersson – 2012-12-05T17:24:46.773@Daniel The
– Nicole Hamilton – 2012-12-05T17:50:57.390source
command is originally from Bill Joy's csh.@NicoleHamilton: Ah, I did not know that. I've only heard it described as a "bashism" in discussions during Debian's great Bash→Dash migration. – Daniel Andersson – 2012-12-05T20:23:06.840