Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Shell script doesn't accept arguments (maybe OT?)

15 views
Skip to first unread message

Tom Furie

unread,
Apr 3, 2023, 11:42:58 AM4/3/23
to
On 2023-04-03, Ottavio Caruso <ottavio2006...@yahoo.com> wrote:
> I have a rudimentary shell launcher:
>
> $ cat opt/bin/ff-accel
> #!/bin/sh
> env MOZ_X11_EGL=1 /home/oc/opt/firefox/firefox
>
> If I launch Firefox normally:
>
> $ /home/oc/opt/firefox/firefox www.google.com
>
> I get the Google homepage as expected.
>
> If I launch it this way:
>
> $ opt/bin/ff-accel www.google.com
>
> I just get a new instance of FF with a blank page.

You're ignoring any arguments you pass to your script.

Cheers,
Tom

Kenny McCormack

unread,
Apr 3, 2023, 12:05:42 PM4/3/23
to
In article <u0erqc$30uas$1...@dont-email.me>,
Ottavio Caruso <ottavio2006...@yahoo.com> wrote:
>Hi,
>
>I'm trying to narrow this down to either my script or Firefox itself.
>
>I have a rudimentary shell launcher:
>
>$ cat opt/bin/ff-accel
>#!/bin/sh
>env MOZ_X11_EGL=1 /home/oc/opt/firefox/firefox

This needs to be:

env MOZ_X11_EGL=1 /home/oc/opt/firefox/firefox "$@"

This a common idiom for passing on to the next script/program whatever was
originally passed to this script.

BTW, why do you use "env"? Is that necessary, or is that just your general
practice?

FWIW, here are 3 alternatives that come to mind:

1) (Easiest) Just set MOZ_X11_EGL in your .profile/.login/.bashrc/whatever
2) Use an alias (tcsh/bash/etc) or function (sh/dash/bash/etc)
3) If you do do it as a script (as shown above), you might as well exec it:

MOZ_X11_EGL=1 exec /home/oc/opt/firefox/firefox "$@"

--
Christianity is not a religion.

- Rick C Hodgin -

Kenny McCormack

unread,
Apr 4, 2023, 5:38:57 AM4/4/23
to
In article <u0go77$3c3o9$2...@dont-email.me>,
Ottavio Caruso <ottavio2006...@yahoo.com> wrote:
>Am 03/04/2023 um 16:05 schrieb Kenny McCormack:
>> In article <u0erqc$30uas$1...@dont-email.me>,
>> Ottavio Caruso <ottavio2006...@yahoo.com> wrote:
>>> Hi,
>>>
>>> I'm trying to narrow this down to either my script or Firefox itself.
>>>
>>> I have a rudimentary shell launcher:
>>>
>>> $ cat opt/bin/ff-accel
>>> #!/bin/sh
>>> env MOZ_X11_EGL=1 /home/oc/opt/firefox/firefox
>>
>> This needs to be:
>>
>> env MOZ_X11_EGL=1 /home/oc/opt/firefox/firefox "$@"
>
>Thanks. I just realised that yesterday night on my way home. But are the
>quotes around $@ necessary?

Yes, if any of any of the args have spaces in them. "$@" is the usual idiom
for this. Read more in "man bash" for the differences between $*, $@,
"$*", and "$@".

>> BTW, why do you use "env"? Is that necessary, or is that just your general
>> practice?
>
>I copied that from here:
>https://wiki.debian.org/Firefox#Hardware_Video_Acceleration
>
>Desktop shortcuts require "env" to work. Do shell scripts not require
>"env" too?

As is the case with such many things, it boils down to whether or not a
shell is involved. I'm assuming that "desktop shortcuts" don't invoke a
shell, so you have to use "env" to get some of the shell functionality.
You don't need it if you already have a shell involved (e.g., in a shell
script).

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/Seriously

Helmut Waitzmann

unread,
Apr 4, 2023, 3:57:31 PM4/4/23
to
Ottavio Caruso <ottavio2006...@yahoo.com>:
> Am 03/04/2023 um 16:05 schrieb Kenny McCormack:
>> In article <u0erqc$30uas$1...@dont-email.me>,
>> Ottavio Caruso <ottavio2006...@yahoo.com> wrote:

>>> I'm trying to narrow this down to either my script or Firefox
>>> itself.
>>>
>>> I have a rudimentary shell launcher:
>>>
>>>
>>> $ cat opt/bin/ff-accel
>>> #!/bin/sh
>>> env MOZ_X11_EGL=1 /home/oc/opt/firefox/firefox
>>
>> This needs to be:
>>
>> env MOZ_X11_EGL=1 /home/oc/opt/firefox/firefox "$@"
>
> Thanks. I just realised that yesterday night on my way home. But
> are the quotes around $@ necessary?

Yes, they are.  The POSIX standard
(<http://www.opengroup.org/onlinepubs/9699919799/mindex.html>)
describes in the volume about the shell command language and the
utilities (follow the "XCU" link,
<https://pubs.opengroup.org/onlinepubs/9699919799/utilities/toc.html>)
what it's going to happen if you don't use the quotes (follow the
"field splitting" link,
<https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05>).

In short:  When not using the quotes, the values of the shell
parameter (in your use case: each of the positional parameters
("$@")) is split into parts at each character which is part of
the set of characters specified by the "IFS" shell variable. 
Depending on further conditions these parts are then matched
against filenames.  This is surely not what you want with your
script which should pass its positional parameters unmodified to
firefox.

=> Using quotes is the "normal way" of passing values of shell
parameters into commands.  (That it is the more complicated
expression than just leaving the quotes away might have its
origin in the historic development of the shell programming
language.)

>> BTW, why do you use "env"? Is that necessary, or is that just
>> your general practice?
>
> I copied that from here:
> https://wiki.debian.org/Firefox#Hardware_Video_Acceleration
>
> Desktop shortcuts require "env" to work. Do shell scripts not
> require "env" too?

The shell has got a built‐in mechanism of specifying environment
variables when invoking a simple command (in
<https://pubs.opengroup.org/onlinepubs/9699919799/utilities/toc.html>
follow the "simple commands" link
<https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01>)
which has got similar capabilities (not exactly the same) like
the «env» program:  To use it in a shell command line, just put
the environment assignments in front of the simple command.

Ed Morton

unread,
Apr 4, 2023, 6:43:46 PM4/4/23
to
On 4/4/2023 3:47 AM, Ottavio Caruso wrote:
> Am 03/04/2023 um 16:05 schrieb Kenny McCormack:
>> In article <u0erqc$30uas$1...@dont-email.me>,
>> Ottavio Caruso  <ottavio2006...@yahoo.com> wrote:
>>> Hi,
>>>
>>> I'm trying to narrow this down to either my script or Firefox itself.
>>>
>>> I have a rudimentary shell launcher:
>>>
>>> $ cat opt/bin/ff-accel
>>> #!/bin/sh
>>> env MOZ_X11_EGL=1 /home/oc/opt/firefox/firefox
>>
>> This needs to be:
>>
>>      env MOZ_X11_EGL=1 /home/oc/opt/firefox/firefox "$@"
>
> Thanks. I just realised that yesterday night on my way home. But are the
> quotes around $@ necessary?

That's the wrong question. The right question would be "is it necessary
to remove the quotes?" because quotes in shell aren't something you add
when you need to, they're something you use by default and only remove
when you need to. If you think about quotes otherwise you'll end up
shooting yourself in the foot one day when you think you don't need
them. Think of quotes like the seatbelt in your car - you shouldn't wait
til you think you might have an accident to put it on. See
https://mywiki.wooledge.org/Quotes for more info.

Ed.

Janis Papanagnou

unread,
Apr 4, 2023, 7:47:27 PM4/4/23
to
On 05.04.2023 00:43, Ed Morton wrote:
> See https://mywiki.wooledge.org/Quotes for more info.

I wonder why that link lists

Backticks: `...` is the legacy command substitution syntax;

under "Quoting"; effectively it's no quoting and has (per se)
nothing to do with quoting (see also the POSIX reference).

And why it says

$"..." : This is a Bash extension.

(and not "a Ksh extension"); didn't Kornshell introduce that?

And why

$'...'

isn't declared as "extension" at all; isn't that a non-standard
(also Ksh-?) extension (that's as well borrowed by other shells)?

But otherwise the link provides some good information.


As a hint for the OP; if studying large texts behind links is
cumbersome I suggest to set the argument list to, say,

set a b "c d" e "f g h" i # here 6 arguments

and just inspect the output of

printf "'%s'\n" "$@"
printf "'%s'\n" "$*"
printf "'%s'\n" $@
printf "'%s'\n" $*

to quickly grasp to what these syntaxes will expand the data;
observe the number or arguments, its grouping, and preservation
of spaces.

Janis

Jalen Q

unread,
Apr 5, 2023, 3:18:56 AM4/5/23
to
ddddvvvddvvd
0 new messages