A here-document is just a document. A script in a here-document is just a script.
Your script could be written
cd "$HOME/own_scripts" || exit 1
./1.sh
./2.sh
Or, if all the scripts that you'd like to invoke are called n.sh
with n
ranging from 1 to, say, 30:
cd "$HOME/own_scripts" || exit 1
for (( i = 1; i <= 30; ++i )); do
[ -x "${i}.sh" ] && ./${i}.sh
done
That is,
source <<-'END_SCRIPT'
cd "$HOME/own_scripts" || exit 1
for (( i = 1; i <= 30; ++i )); do
[ -x "${i}.sh" ] && ./${i}.sh
done
END_SCRIPT
In fact, since the here-document now is pretty short, you could get rid of the cd
again:
source <<-'END_SCRIPT'
for (( i = 1; i <= 30; ++i )); do
[ -x "$HOME/own_scripts/${i}.sh" ] && "$HOME/own_scripts/${i}.sh"
done
END_SCRIPT
Only, source
does not read from standard input... so sourcing the here-document won't work to start with.
Instead, use something like
bash <<-'END_SCRIPT'
...as above...
END_SCRIPT
(if bash
is the shell you want to run the script with).
I've used 'END_SCRIPT'
to start the here-document. The quoting stops the current shell from expanding variables within the document, and the more verbose tag serves as documentation to the reader.
I've used $HOME
rather than ~
since tilde does not behave like a variable and the more verbose $HOME
serves better as documentation in scripts.
The script in the here-document simply uses cd
to change directory to the $HOME/onw_script
directory. If that fails, the shell session is terminated.
In the examples with the for
-loop (which will work in bash
and ksh
shells), I generate the name of the script to execute in the loop. I also test whether there's actually an executable file available with that name before trying to execute it (with th -x
test).
bash: source: filename argument required
to standard error, so you've probably got something else to fix first, but I don't think I understand what the other issue you're really asking about is. – Michael Homer Feb 03 '18 at 06:13. ~/own_scripts/1.sh
is shorter than that. Orfor i in {1..6}; do source ~/own_scripts/$i.sh; done
orfor f in ./{a,b}.sh ; do source $f; done
. If you need to activate and deactivate it without commenting always 10, 20 lines out, put the call into a function, and activate/deactivate the function call only. – user unknown Feb 03 '18 at 06:33$HOME/onw_scripts
are executing in (the current shell environment or their own shell environment). – Kusalananda Feb 03 '18 at 07:26