sanjay kumar via Bug reports for the GNU Bourne Again SHell
<bug-bash@gnu.org> writes:

Somehow all the line breaks in your report were lost.  As I reconstruct
it, you wrote:

    > cat test.sh
    #!/bin/bash
    echo "argv[0] = ${0}"
    echo "argv[1] = ${1}"
    echo "argv[2] = ${2}"
    echo "count = ${#}"
    ===================
    > bash -c ./test.sh abc def
    argv[0] = ./test.sh
    argv[1] =
    argv[2] =
    count = 0
    ===================
    > bash ./test.sh abc def
    argv[0] = ./test.sh
    argv[1] = abc
    argv[2] = def
    count = 2
    ===================
    > bash -version
    GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
    Copyright (C) 2019 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later 
<http://gnu.org/licenses/gpl.html>
    This is free software; you are free to change and redistribute it.There is 
NO WARRANTY, to the extent permitted by law.

The problem is a little subtle.  More exactly, the reason that the first
example didn't produce an obvious error is a little subtle.  The
definition of "-c" is "commands are read from the first non-option
argument command_string.  If there are arguments after the
command_string, the first argument is assigned to $0 and any remaining
arguments are assigned to the positional parameters."

So the first step in processing "bash -c ./test.sh abc def" is that Bash
assigns "abc" to $0 and "def" to $1.  Then Bash starts processing
commands from the string "./test.sh".  Assuming that ./test.sh has
execute permission, "./test.sh" is a valid command line, and Bash then
executes ./test.sh.

That command, however, has no arguments, so ./test.sh produces the
output that you see.

Dale

Reply via email to