help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: -c option not working as expected


From: Andreas Kähäri
Subject: Re: -c option not working as expected
Date: Sat, 25 Jan 2025 10:51:43 +0100

Je Sat, Jan 25, 2025 at 09:36:20AM +0000, sanjay kumar skribis:
>  https://www.gnu.org/software/bash/manual/bash.html#Invoking-Bash Says ->
>    
>    - -c
>       -    
> Read and execute commands from the first non-option argument command_string, 
> then exit. 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. The assignment to $0 sets the name of the shell, which 
> is used in warning and error messages.
> 
> 
> So, bash -c is not behaving as mentioned in above document.

It is.  The first non-option argument, the command string, is "./test.sh".  
There are arguments after the command string, "abc" and "def".  The first 
argument is assigned to $0, and the remaining arguments are assigned to the 
positional parameters.

Note that the command string is not the script file name, but the
contents of the script that "bash -c" runs.   The command string is
executed with $0 and $1 set to "abc" and "def", respectively.

Try this:

        bash -c 'echo "$0" "$1" "$2"; ./test.sh' 0arg abc def

... then this:

        bash -c 'echo "$0" "$1" "$2"; ./test.sh "$@"' 0arg abc def



> RegardsSanjay
>     On Saturday, January 25, 2025 at 02:49:13 PM GMT+5:30, Andreas Kähäri 
> <andreas.kahari@abc.se> wrote:  
>  
>  Je Sat, Jan 25, 2025 at 08:57:18AM +0000, sanjay kumar via Bug reports for 
> the GNU Bourne Again SHell skribis:
> > > cat test.sh#!/bin/bashecho "argv[0] = ${0}"echo "argv[1] = ${1}"echo 
> > > "argv[2] = ${2}"echo "count = ${#}"===================> bash -c  
> > > ./test.sh abc defargv[0] = ./test.shargv[1] =argv[2] =count = 
> > > 0===================> bash  ./test.sh abc defargv[0] = ./test.shargv[1] = 
> > > abcargv[2] = defcount = 2===================> bash -versionGNU 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.
> > 
> > Looking for input.
> > Regards
> > Sanjay
> > 
> 
> 
> When you execute
> 
>     bash -c ./test.sh abc def
> 
> ... the bash shell that you invoke is snarted with argv[0] set to "abc"
> and argv[1] set to "def".  The contents of the script is "./test.sh",
> and that command is invoked *without* any arguments.
> 
> When you execute
> 
>     bash ./test.sh abc def
> 
> ... the bash shell that you invoke is started with argv[1] set to "abc"
> and argv[2] set to "def".  The argv[0] string is set to "./test.sh".
> The contents of the script is the contents of the file "./test.sh", and
> that command is invoked *with* the arguments "abc" and "def".
> 
> 
> -- 
> Andreas (Kusalananda) Kähäri
> Uppsala, Sweden
> 
> .
>   

-- 
Andreas (Kusalananda) Kähäri
Uppsala, Sweden

.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]