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

$ foo=bar command cmd [arg ...]

38 views
Skip to first unread message

hongy...@gmail.com

unread,
Dec 21, 2020, 9:08:08 PM12/21/20
to
When using command to run a cmd, I want to pass variables to it as shown below:

$ foo=bar command cmd [arg ...]

I'm not sure whether this is possible or not. Any hints will be highly appreciated.

Regards,
HY

Javier

unread,
Dec 21, 2020, 9:37:20 PM12/21/20
to
You can do that. When there is more than 1 or 2 options using variables
makes faster writing code instead of writing a CLI parser with the
getopts builtin or getopt(1). I do that very often for scripts that
*I only use myself*.

However this article considers using variables for setting options as a bad
practice.

http://www.shelldorado.com/goodcoding/cmdargs.html

Aside of user friendliness and feature discoverability the biggest
problem that I can see is that you may export variables inadvertedly
before you invoke your script and when you invoke your script you have
set the variable for that special option inadvertedly shooting yourself
in the foot. It happened to me once.

To minimize that risk use variable names that are lower case.
Upper case is as a convention used for exported variables.

Keith Thompson

unread,
Dec 21, 2020, 10:26:46 PM12/21/20
to
Javier <inv...@invalid.invalid> writes:
> hongy...@gmail.com <hongy...@gmail.com> wrote:
>> When using command to run a cmd, I want to pass variables to it as
>> shown below:
>>
>> $ foo=bar command cmd [arg ...]
>>
>> I'm not sure whether this is possible or not. Any hints will be
>> highly appreciated.
>
> You can do that. When there is more than 1 or 2 options using variables
> makes faster writing code instead of writing a CLI parser with the
> getopts builtin or getopt(1). I do that very often for scripts that
> *I only use myself*.
[...]

I think you missed the point of the question. The OP is asking about
the "command" command, a built-in command in bash (and probably other
shells, I haven't checked), not about commands in general.

--
Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

hongy...@gmail.com

unread,
Dec 22, 2020, 1:05:55 AM12/22/20
to
On Tuesday, December 22, 2020 at 11:26:46 AM UTC+8, Keith Thompson wrote:
> Javier <inv...@invalid.invalid> writes:
> > hongy...@gmail.com <hongy...@gmail.com> wrote:
> >> When using command to run a cmd, I want to pass variables to it as
> >> shown below:
> >>
> >> $ foo=bar command cmd [arg ...]
> >>
> >> I'm not sure whether this is possible or not. Any hints will be
> >> highly appreciated.
> >
> > You can do that. When there is more than 1 or 2 options using variables
> > makes faster writing code instead of writing a CLI parser with the
> > getopts builtin or getopt(1). I do that very often for scripts that
> > *I only use myself*.
> [...]
>
> I think you missed the point of the question. The OP is asking about
> the "command" command, a built-in command in bash (and probably other
> shells, I haven't checked), not about commands in general.

Yes, that's what exactly I mean.

Javier

unread,
Dec 22, 2020, 8:34:53 AM12/22/20
to
Let's try that directly with an example

cd $(mktemp -d)
echo '#!/usr/bin/env bash' > cmd.sh
echo 'echo foo is $foo' >> cmd.sh
chmod a+x cmd.sh
foo=bar command ./cmd.sh

It works for me, so what you propose is possible (at least in bash).

Javier

unread,
Dec 22, 2020, 8:44:24 AM12/22/20
to
Keith Thompson <Keith.S.T...@gmail.com> wrote:
> I think you missed the point of the question. The OP is asking about
> the "command" command, a built-in command in bash (and probably other
> shells, I haven't checked), not about commands in general.

Yes, I totally missed the point of the question. I misread 'command
cmd' as a single word. I have now posted another reply to HZ.

In any case, I hope that the other readers of the group find my misled
message about using vars instead of command line arguments interesting.

hongy...@gmail.com

unread,
Dec 22, 2020, 9:50:04 AM12/22/20
to
Really, it also works for me. Thanks a lot for your wonderful example.
0 new messages