Imagine you have a silly script test.sh to which arguments are passed that would look like this:

bash test.sh arg1 arg2 arg3

with test.sh being a silly script that displays its command line:

echo "$0 $*"

I would like to do the same using bash heredoc << to feed the script to bash. So I tried this :

bash <<'EOF' -- arg1 arg2 arg3
echo "$0 $*"

But it fails with error

bash: arg1: No such file or directory

Any idea ?

2 Answers 2


The here document is passed to the inner bash on its standard input. You need to instruct bash to read from standard input. With no command line arguments, this happens automatically. If there's at least one non-option argument and no -s or -c option, the first non-option argument is the name of the script file to run. You can pass the -s option to tell bash to read from standard input.

bash <<'EOF' -s -- arg1 arg2 arg3
echo "$0 $*"
  • Hmmmm, if that's the case then maybe I'm better off doing echo arg1 | bash <<EOF unless I'm not understanding that they're different. Apr 2 at 3:55

Alternatively, on most systems, you can do:

bash /dev/fd/3 3<< 'EOF' arg1 arg2 arg3
echo "$0 $*"
IFS= read -rp "Enter something: "
printf 'You entered: "%s"\n' "$REPLY"

That way, your here-script can still access stdin.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .