You are not logged in.

#1 2014-12-31 17:00:09

souenzzo
Member
Registered: 2012-10-31
Posts: 35

[SOLVED!][bash] unexpected result on passing variable as argument

Hi all

SOLUTION ON 2 FIRST REPLIES

Why it not work?

#!/usr/bin/bash
PKG=gst-plugins-{bad,base,good,ugly}
# With {,double}quotes also not work
# PKG="gst-plugins-{bad,base,good,ugly}"
# PKG='gst-plugins-{bad,base,good,ugly}'

pacman -S $PKG
## Don't work!
# error: target not found: gst-plugins-{bad,base,good,ugly}

pacman -S gst-plugins-{bad,base,good,ugly}
#Works fine!

How can I pass an array/list/idonknownameofthat as argument to pacman(and other progams)?

[edit] ~> a better title smile

Last edited by souenzzo (2014-12-31 22:00:18)

Offline

#2 2014-12-31 17:33:44

souenzzo
Member
Registered: 2012-10-31
Posts: 35

Re: [SOLVED!][bash] unexpected result on passing variable as argument

Solution 1:

#!/usr/bin/bash
PKG=gst-plugins-{bad,base,good,ugly}

eval pacman -S $PKG
# Works fine too!

Last edited by souenzzo (2014-12-31 22:06:55)

Offline

#3 2014-12-31 18:35:15

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,331
Website

Re: [SOLVED!][bash] unexpected result on passing variable as argument

Or let the expansion happen in the setting of the variable:

PKG="$(echo gst-plugins-{bad,base,good,ugly})"

"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#4 2014-12-31 22:05:35

souenzzo
Member
Registered: 2012-10-31
Posts: 35

Re: [SOLVED!][bash] unexpected result on passing variable as argument

Or a mixed solution to allow any input on your script

#!/usr/bin/bash
PKG=gst-plugins-{bad,base,good,ugly}

PKG=$(eval echo $PKG)  ## Edit: Don't use. See below  ##

pacman -S $PKG
# Works fine too!

Thanks!

Last edited by souenzzo (2015-01-01 14:49:49)

Offline

#5 2015-01-01 00:47:36

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED!][bash] unexpected result on passing variable as argument

`eval echo`? Is that really necessary?

┌─[Shiv ~ ]
└─╼ PKG=(gst-plugins-{bad,base,good,ugly})
┌─[Shiv ~ ]
└─╼ printf "%s\n" "${PKG[@]}"
gst-plugins-bad
gst-plugins-base
gst-plugins-good
gst-plugins-ugly

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#6 2015-01-01 03:23:08

souenzzo
Member
Registered: 2012-10-31
Posts: 35

Re: [SOLVED!][bash] unexpected result on passing variable as argument

It's works too!
But that NEED the parenthesis. In my case, I want an "aways work"/"easy input". I think "eval echo" fits better.

Offline

#7 2015-01-01 03:37:08

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED!][bash] unexpected result on passing variable as argument

souenzzo wrote:

It's works too!
But that NEED the parenthesis. In my case, I want an "aways work"/"easy input". I think "eval echo" fits better.

It "needs" the parentheses because it is initialising an array. And trust me, anything would be better than `eval echo`...
See http://mywiki.wooledge.org/BashFAQ/048


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB