Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"If I do not want others to quote me, I do not speak." -- Phil Wayne


devel / comp.unix.shell / Re: putting "$@" in variable

SubjectAuthor
* putting "$@" in variableStefan W
`* Re: putting "$@" in variableJanis Papanagnou
 `* Re: putting "$@" in variableSpiros Bousbouras
  `* Re: putting "$@" in variableJanis Papanagnou
   `* Re: putting "$@" in variableglenn stevenson
    `* Re: putting "$@" in variableJanis Papanagnou
     `- Re: putting "$@" in variableGeoff Clare

1
putting "$@" in variable

<so7ubj$66p$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4647&group=comp.unix.shell#4647

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!w47NHmGDR3kdEdWIugiGZw.user.46.165.242.91.POSTED!not-for-mail
From: viste...@mail.ru (Stefan W)
Newsgroups: comp.unix.shell
Subject: putting "$@" in variable
Date: Wed, 1 Dec 2021 16:46:26 +0300
Organization: Aioe.org NNTP Server
Message-ID: <so7ubj$66p$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="6361"; posting-host="w47NHmGDR3kdEdWIugiGZw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.3.2
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Stefan W - Wed, 1 Dec 2021 13:46 UTC

Hi! I'm writing very ordinary script. It processes some standard list of
files being called with no arguments, and it handles passed files if
there are arguments. Nothing strange.

I'm know that correct iteration of list of arguments in bash could be
simple as:

for i in "$@"; do
echo $i
done

being called like:

> ./test.sh 1 2 '3 4'

it gives correct output

1
2 3 4

But if I put in into variable, things got worse:

files="$@"

for i in $files; do
echo $i
done

> ./test.sh 1 2 '3 4'

1
2 3
4

I know that it is possible to use `if` and iterate with `for arg in
"$@";`. But I want to have only one universal algorithm for file
processing, and just prepare a list of files and then pass it to function.

How could I have correct list of filenames (probably containing spaces)
in correctly iterable variable in bash?

Re: putting "$@" in variable

<so81kl$l4l$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4648&group=comp.unix.shell#4648

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: putting "$@" in variable
Date: Wed, 1 Dec 2021 15:42:29 +0100
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <so81kl$l4l$1@dont-email.me>
References: <so7ubj$66p$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 1 Dec 2021 14:42:29 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="d2ed0fc56fc78b58108a8adc7257c0a3";
logging-data="21653"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+tNgr05g/D4szLny+r1pfu"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:W/f94RCC1cQpLvbUtYH9z2mcWbA=
In-Reply-To: <so7ubj$66p$1@gioia.aioe.org>
 by: Janis Papanagnou - Wed, 1 Dec 2021 14:42 UTC

On 01.12.2021 14:46, Stefan W wrote:
> Hi! I'm writing very ordinary script. It processes some standard list of
> files being called with no arguments, and it handles passed files if
> there are arguments. Nothing strange.
>
> I'm know that correct iteration of list of arguments in bash could be
> simple as:
>
> for i in "$@"; do
> echo $i
> done
>
> [...]
>
> But if I put in into variable, things got worse:
>
> files="$@"
>
> for i in $files; do
> echo $i
> done
>
> [...]
>
> I know that it is possible to use `if` and iterate with `for arg in
> "$@";`. But I want to have only one universal algorithm for file
> processing, and just prepare a list of files and then pass it to function.
>
> How could I have correct list of filenames (probably containing spaces)
> in correctly iterable variable in bash?

Assigning to simple scalar variables make the program parameters flat,
that's hardly avoidable.

If you want to keep an existing array structure intact then use shell
arrays as in

files=( "$@" )

for i in "${files[@]}"; do
echo $i
done

Janis

Re: putting "$@" in variable

<2jSGKKRhDPCp0UPX5@bongo-ra.co>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4649&group=comp.unix.shell#4649

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!NFyD4+LU+vfu+oVKTmfrbQ.user.46.165.242.91.POSTED!not-for-mail
From: spi...@gmail.com (Spiros Bousbouras)
Newsgroups: comp.unix.shell
Subject: Re: putting "$@" in variable
Date: Wed, 1 Dec 2021 15:04:34 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <2jSGKKRhDPCp0UPX5@bongo-ra.co>
References: <so7ubj$66p$1@gioia.aioe.org> <so81kl$l4l$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="18516"; posting-host="NFyD4+LU+vfu+oVKTmfrbQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Organisation: Weyland-Yutani
X-Server-Commands: nowebcancel
X-Notice: Filtered by postfilter v. 0.9.2
 by: Spiros Bousbouras - Wed, 1 Dec 2021 15:04 UTC

On Wed, 1 Dec 2021 15:42:29 +0100
Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
> On 01.12.2021 14:46, Stefan W wrote:
> > Hi! I'm writing very ordinary script. It processes some standard list of
> > files being called with no arguments, and it handles passed files if
> > there are arguments. Nothing strange.
> >
> > I'm know that correct iteration of list of arguments in bash could be
> > simple as:
> >
> > for i in "$@"; do
> > echo $i
> > done
> >
> > [...]
> >
> > But if I put in into variable, things got worse:
> >
> > files="$@"
> >
> > for i in $files; do
> > echo $i
> > done
> >
> > [...]
> >
> > I know that it is possible to use `if` and iterate with `for arg in
> > "$@";`. But I want to have only one universal algorithm for file
> > processing, and just prepare a list of files and then pass it to function.
> >
> > How could I have correct list of filenames (probably containing spaces)
> > in correctly iterable variable in bash?
>
> Assigning to simple scalar variables make the program parameters flat,
> that's hardly avoidable.
>
> If you want to keep an existing array structure intact then use shell
> arrays as in
>
> files=( "$@" )
>
> for i in "${files[@]}"; do
> echo $i
> done

Surely that should be echo "$i"

Re: putting "$@" in variable

<so8q0t$gi8$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4654&group=comp.unix.shell#4654

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: putting "$@" in variable
Date: Wed, 1 Dec 2021 22:38:37 +0100
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <so8q0t$gi8$1@dont-email.me>
References: <so7ubj$66p$1@gioia.aioe.org> <so81kl$l4l$1@dont-email.me>
<2jSGKKRhDPCp0UPX5@bongo-ra.co>
Mime-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 1 Dec 2021 21:38:37 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="d2ed0fc56fc78b58108a8adc7257c0a3";
logging-data="16968"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18we1k1lnbx7gPF927F2b69"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:cGnyD1KHv2li4R/pQCw8g+t3cx0=
In-Reply-To: <2jSGKKRhDPCp0UPX5@bongo-ra.co>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Wed, 1 Dec 2021 21:38 UTC

On 01.12.2021 16:04, Spiros Bousbouras wrote:
> On Wed, 1 Dec 2021 15:42:29 +0100
> Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
>> On 01.12.2021 14:46, Stefan W wrote:
>>> [...]
>>
>> files=( "$@" )
>>
>> for i in "${files[@]}"; do
>> echo $i
>> done
>
> Surely that should be echo "$i"

I considered that just as test output and left it unchanged,
but surely you are right to better have a correctly quoted
paragon and quote it anyway, I agree. My fault.

Janis

Re: putting "$@" in variable

<e42fff6a-4857-4c09-b5ee-e8a30c0b8dfdn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=5196&group=comp.unix.shell#5196

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:622a:13ca:b0:2e1:a52f:18f4 with SMTP id p10-20020a05622a13ca00b002e1a52f18f4mr18899476qtk.412.1648420954573;
Sun, 27 Mar 2022 15:42:34 -0700 (PDT)
X-Received: by 2002:a05:622a:256:b0:2e1:a01b:a538 with SMTP id
c22-20020a05622a025600b002e1a01ba538mr19384800qtx.167.1648420954349; Sun, 27
Mar 2022 15:42:34 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!nntp.club.cc.cmu.edu!45.76.7.193.MISMATCH!3.us.feeder.erje.net!1.us.feeder.erje.net!feeder.erje.net!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sun, 27 Mar 2022 15:42:34 -0700 (PDT)
In-Reply-To: <so8q0t$gi8$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=193.176.30.8; posting-account=kihP-goAAAArC7ajp60ZjVcAplynltT6
NNTP-Posting-Host: 193.176.30.8
References: <so7ubj$66p$1@gioia.aioe.org> <so81kl$l4l$1@dont-email.me>
<2jSGKKRhDPCp0UPX5@bongo-ra.co> <so8q0t$gi8$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <e42fff6a-4857-4c09-b5ee-e8a30c0b8dfdn@googlegroups.com>
Subject: Re: putting "$@" in variable
From: hunkemol...@gmail.com (glenn stevenson)
Injection-Date: Sun, 27 Mar 2022 22:42:34 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 26
 by: glenn stevenson - Sun, 27 Mar 2022 22:42 UTC

On Wednesday, December 1, 2021 at 9:38:42 PM UTC, Janis Papanagnou wrote:
> On 01.12.2021 16:04, Spiros Bousbouras wrote:
> > On Wed, 1 Dec 2021 15:42:29 +0100
> > Janis Papanagnou <janis_pa...@hotmail.com> wrote:
> >> On 01.12.2021 14:46, Stefan W wrote:
> >>> [...]
> >>
> >> files=( "$@" )
> >>
> >> for i in "${files[@]}"; do
> >> echo $i
> >> done
> >
> > Surely that should be echo "$i"
> I considered that just as test output and left it unchanged,
> but surely you are right to better have a correctly quoted
> paragon and quote it anyway, I agree. My fault.
>
> Janis

here's a worked example .... - use quotes to avoid any potential shell expansion

for file in 1 2 3 '3 4'; do x="${file}";echo "<${x}>"; done
<1>
<2>
<3>
<3 4>

Re: putting "$@" in variable

<t1qqgd$1hc$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=5197&group=comp.unix.shell#5197

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: putting "$@" in variable
Date: Mon, 28 Mar 2022 01:03:40 +0200
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <t1qqgd$1hc$1@dont-email.me>
References: <so7ubj$66p$1@gioia.aioe.org> <so81kl$l4l$1@dont-email.me>
<2jSGKKRhDPCp0UPX5@bongo-ra.co> <so8q0t$gi8$1@dont-email.me>
<e42fff6a-4857-4c09-b5ee-e8a30c0b8dfdn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 27 Mar 2022 23:03:41 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="087c11794ed48240cf355c25ea5ae8fc";
logging-data="1580"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18bycSmGLi7b3m4pAKGTA94"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:Ctu6xt6nwckGs7gopev35thYgrs=
In-Reply-To: <e42fff6a-4857-4c09-b5ee-e8a30c0b8dfdn@googlegroups.com>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Sun, 27 Mar 2022 23:03 UTC

On 28.03.2022 00:42, glenn stevenson wrote:
> On Wednesday, December 1, 2021 at 9:38:42 PM UTC, Janis Papanagnou wrote:
>> On 01.12.2021 16:04, Spiros Bousbouras wrote:
>>> On Wed, 1 Dec 2021 15:42:29 +0100
>>> Janis Papanagnou <janis_pa...@hotmail.com> wrote:
>>>> On 01.12.2021 14:46, Stefan W wrote:
>>>>> [...]
>>>>
>>>> files=( "$@" )
>>>>
>>>> for i in "${files[@]}"; do
>>>> echo $i
>>>> done
>>>
>>> Surely that should be echo "$i"
>> I considered that just as test output and left it unchanged,
>> but surely you are right to better have a correctly quoted
>> paragon and quote it anyway, I agree. My fault.
>>
>> Janis
>
> here's a worked example .... - use quotes to avoid any potential shell expansion
>
> for file in 1 2 3 '3 4'; do x="${file}";echo "<${x}>"; done
> <1>
> <2>
> <3>
> <3 4>
>

This is a bad example - what happens if you omit the quotes here?

$ for file in 1 2 3 '3 4' ; do x=${file} ; echo +${x}+ ; done
+1+
+2+
+3+
+3 4+

The variables ${file] and ${x} are still correctly expanded.

As a maybe better example, below the quotes around ${x} are necessary:

$ for file in 1 2 3 '3 4'; do x=${file}; printf "'%s'\n" "${x}"; done

Of course, to demonstrate the expansion, this suffices:

$ for x in 1 2 3 '3 4'; do printf "'%s'\n" "${x}"; done

since the assignment x=${file} doesn't need quotes and can be omitted
for the example.

Janis

Re: putting "$@" in variable

<asabhi-408.ln1@ID-313840.user.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=5204&group=comp.unix.shell#5204

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: geo...@clare.See-My-Signature.invalid (Geoff Clare)
Newsgroups: comp.unix.shell
Subject: Re: putting "$@" in variable
Date: Tue, 29 Mar 2022 13:32:42 +0100
Lines: 30
Message-ID: <asabhi-408.ln1@ID-313840.user.individual.net>
References: <so7ubj$66p$1@gioia.aioe.org> <so81kl$l4l$1@dont-email.me>
<2jSGKKRhDPCp0UPX5@bongo-ra.co> <so8q0t$gi8$1@dont-email.me>
<e42fff6a-4857-4c09-b5ee-e8a30c0b8dfdn@googlegroups.com>
<t1qqgd$1hc$1@dont-email.me>
Reply-To: netnews@gclare.org.uk
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net KKP2n6/RSK3HLAD1NobcKgY3SRF5eCH0PFkBQY0kxVY0N3cc26
X-Orig-Path: ID-313840.user.individual.net!not-for-mail
Cancel-Lock: sha1:UGhr4X0agnYrkPT6KMVxqVUe0DU=
User-Agent: Pan/0.145 (Duplicitous mercenary valetism; d7e168a
git.gnome.org/pan2)
 by: Geoff Clare - Tue, 29 Mar 2022 12:32 UTC

Janis Papanagnou wrote:

> On 28.03.2022 00:42, glenn stevenson wrote:

>> here's a worked example .... - use quotes to avoid any potential shell expansion
>>
>> for file in 1 2 3 '3 4'; do x="${file}";echo "<${x}>"; done
>> <1>
>> <2>
>> <3>
>> <3 4>
>>
>
> This is a bad example - what happens if you omit the quotes here?
>
> $ for file in 1 2 3 '3 4' ; do x=${file} ; echo +${x}+ ; done
> +1+
> +2+
> +3+
> +3 4+
>
> The variables ${file] and ${x} are still correctly expanded.

Nit-pick: +${x}+ is not correctly expanded, it's just that the behaviour
of echo with multiple arguments hides the problem because it produces
the same output. (As you point out later, this can be avoided by
doing such tests with printf instead of echo.)

--
Geoff Clare <netnews@gclare.org.uk>

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor