Nested `source` commands in my bash script?

bash nested functions
bash get script directory
bash script path
bash get directory of file

I have the following setup

└── modules
    ├── sub-modules
    │   └──
  • and I have added the path to /modules to $PATH.


source sub-module/

But when I run this I get:

-bash: /sub-module/ No such file or directory

Yes. This is how the ./source operator works.

From the spec:

The shell shall execute commands from the file in the current environment.

If file does not contain a <slash>, the shell shall use the search path specified by PATH to find the directory containing file. Unlike normal command search, however, the file searched for by the dot utility need not be executable. If no readable file is found, a non-interactive shell shall abort; an interactive shell shall write a diagnostic message to standard error, but this condition shall not be considered a syntax error.

So when you use source the shell searches in the $PATH but when you use source dir/ the $PATH lookup is skipped and dir is assumed to be relative to the current directory.

To do this sort of thing (internally to or you could use Can a Bash script tell what directory it's stored in? to find the appropriate absolute path to use.

For general use you are going to need to stick them all in $PATH or source them from absolute/etc. paths.

Nested source of bash script doesn't work · Issue #1293 · xonsh , Nested source of bash script doesn't work #1293 completely unrelated to the source-foreign commands, except that the happen to use the  Hello! Below my first bash script. As you can see i build 2 nested cases. The second one (sync databases) is working fine. Bu the first one (sync datadirs) is not working. It says: rsync: command not found. However when i move the rsync command to the top of the script its working. So i suppose (2 Replies)

Path lookups only occur on names that don't contain a /. Also, only the immediate contents of a directory in PATH is checked, not all its subdirectories. With source sub-module/, sub-module must be in the current directory. You would have to add /path/to/modules/sub-module to your path as well, then source alone.

You can keep that to one line if you want:

PATH=/path/to/modules/sub-module:$PATH source

This only updates PATH for this single source command.

Bash: Nested functions and including other scripts, Hello. I have the following problem with bash code: pre { overflow:scroll; margin:​2px; padding:15px; Loading command handler file and calling handler source​  As everybody says, bash doesn't have a proper language-supported try/catch syntax. You can launch bash with the -e argument or use set -e inside the script to abort the entire bash process if any command has a non-zero exit code. (You can also set +e to temporarily allow failing commands.)


export ROOT="${BASH_SOURCE%/*}"
source "${ROOT}/modules/"


source "${ROOT}/modules/sub-modules/"

$PATH doesn't have to be changed for this to work. For potential $BASH_SOURCE caveats, see BashFAQ/028 .

Unable to assign output of nested commands to variable in bash , to assign the result of a command to a variable we use $() (the Here I'm selecting words from /usr/share/dict/words, just as a source of text. To see the program's math in action, I wrote up a wrapper script that iterates through different line  I've been working on trying to automate the complicated process of building source code on a build machine and then transferring the compiled image files over to my embedded ARMv7 device to be flashed. Each step by itself is easy to automate with standard Linux Shell Script, but when trying to do e

Advanced Bash-Scripting Guide, System administration relies a great deal on shell scripting; common tasks are often automated using simple scripts. A shell that you get when logging in to the console in text mode. A shell obtained Command substitutions may be nested. Single command output to a variable. Bash commands can be used without any option and argument for those commands where these parts are optional. The following two examples show the uses of simple command substitution. Example#1: bash `date` command is used to show the current date and time.

Bash Guide for Beginners, A shell scripting is writing a program for the shell to execute and a shell It provides bash command to execute Bash script files. fi # end of nested if block To source an external Bash script, we use source command and  42. Replace order of two words in a text that match a pattern. The following `sed` command will take the input of two words from `echo` command and replace the order of these words. $ echo "perl python" | sed -e 's/\ ( [^ ]*\) *\ ( [^ ]*\)/\2 \1/'.

Bash Scripting: Everything you need to know about Bash-shell , 10.9.4 0:00.03elapsed The total elapsed time for the command to run Preceding the nested script with the built-in C shell command source or the Korn shell  A loop statement can use any other type of command within the loop, including other loop commands. This is called a nested loop.Care should be taken when using nested loops, because you’re performing an iteration within an iteration, which multiplies the number of times commands are being run.