bug-bash
[Top][All Lists]
Advanced

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

Re: Is it normal for `bash -s foo` not to make 1=foo available from ~/.b


From: Torka Noda
Subject: Re: Is it normal for `bash -s foo` not to make 1=foo available from ~/.bashrc?
Date: Wed, 29 Mar 2017 16:10:14 +0200

On Tue, 28 Mar 2017 16:43:17 -0400
Chet Ramey <chet.ramey@case.edu> wrote:
>
> On 3/28/17 12:43 PM, Torka Noda wrote:
> 
> > Actually, shouldn't `bash -s`, without any command fed to
> > its stdin, exit immediately, anyway...?  
> 
> No. Why? It reads and executes commands from its standard
> input, which is in most cases, the terminal.  However, `bash
> -s foo </dev/null' should exit immediately.
> 


... Because I misread the manual, forgot the "when invoking an
interactive shell" part, and didn't do enough tests to
compensate :)


I thought the following command would print "foo bar", as in
"it would set the positional parameters (as in 'arguments') for
the commands themselves, fed to Bash's stdin on start":

========================================
$ echo 'echo' | bash -s foo bar

$
========================================

Instead it prints nothing, and only sets Bash's positional
parameters, so I could use the following command to get that
result:

========================================
$ echo 'echo $1 $2' | bash -s foo bar
foo bar
$
========================================


The same goes for `bash -c`:

========================================
$ bash -c 'echo' foo bar

$ bash -c 'echo $0 $1 $2' bash foo bar
bash foo bar
========================================


I now understand better what Daniel Mills was saying...

... except that `echo 'echo $1 $2' | bash -s foo bar` would not
read ~/.bashrc, because in this case the shell is not
interactive, so no possible modification of the positional
parameters specified on the command-line, unless the shell
is fully interactive, in which case we can already use `set --
foo bar` whenever we want, including from initialization
files...

... and I'm not sure that, philosophically, command-line
arguments are supposed to keep the priority, over
active initialization files (compared to plain configuration
files, which should indeed be overridden by the command-line).
And they currently don't, anyway.


So, in the end, we can set Bash's positional parameters from
~/.bashrc, and use them then, and they'll override the
command-line specified ones in interactive shells, but we
can never access the original ones from ~/.bashrc...

Also, `bash -s foo bar` is actually the proper way to do what I
want, then... It *is* supposed to set Bash's positional
parameters, not the ones of the commands fed to its stdin...
The only problem is that they are not accessible from
initialization files...


Well, sorry for the confusion, I'll stop here. I think it's
weird for Bash's positional parameters, and the whole argument
list if modified with '-s', not to be accessible from
initialization files, but `env` does what I want relatively
simply compared to the tricks used by other people on the web,
and I'll be content with that.



reply via email to

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