I have a script print.sh:
#!/bin/bash
echo printing provided args:
for i in "$@"; do
echo -e "\t${i}"
done
If I do this at the prompt a=$(. print.sh ); echo "${a}"
then I get printing provided args:
as the stored output
My main script to test command substitution looks like this:
#!/bin/bash
function func_to_call_sub_scrp
{
# Call sub-script
capture="$(. $1 $2 $3)"
echo -e "captured output:
\t\t${capture}"
}
echo "Run function to call sub script without parameters passed. "
func_to_call_sub_scrp print.sh
echo ""
echo "Run function to call sub script with parameters passed."
func_to_call_sub_scrp print.sh xx yy
This outputs:
Run function to call sub script without parameters passed.
captured output:
printing provided args:
print.sh
Run function to call sub script with parameters passed.
captured output:
printing provided args:
xx
yy
The second call sends xx and yy to print.sh which is as expected. However when I send "print.sh" "" "" command substitution calls print.sh and sends it print.sh as its $1 instead of sending it "" and ""
My question is how is $1 being passed as an argument to the printing script when there are no other inputs? I expected "$(. $1 $2 $3)"
to become "$(. print.sh )"
or "printing provided args:"
once returned.
.
), you're providing them again. At a bash prompt, typehelp .
– glenn jackman Jan 10 '22 at 16:45