Run a command using arguments that come from an array
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
Suppose I have a graphical program named app
. Usage example: app -t 'first tab' -t 'second tab'
opens two 'tabs' in that program.
The question is: how can I execute the command (i.e. app
) from within a bash
script if the number of arguments can vary?
Consider this:
#!/bin/bash
tabs=(
'first tab'
'second tab'
)
# Open the app (starting with some tabs).
appÃÂ # ... How to get `app -t 'first tab' -t 'second tab'`?
I would like the above script to have an effect equivalent to app -t 'first tab' -t 'second tab'
. How can such a bash script be written?
Edit: note that the question is asking about composing command line arguments on the fly using an array of arguments.
bash
add a comment |Â
up vote
3
down vote
favorite
Suppose I have a graphical program named app
. Usage example: app -t 'first tab' -t 'second tab'
opens two 'tabs' in that program.
The question is: how can I execute the command (i.e. app
) from within a bash
script if the number of arguments can vary?
Consider this:
#!/bin/bash
tabs=(
'first tab'
'second tab'
)
# Open the app (starting with some tabs).
appÃÂ # ... How to get `app -t 'first tab' -t 'second tab'`?
I would like the above script to have an effect equivalent to app -t 'first tab' -t 'second tab'
. How can such a bash script be written?
Edit: note that the question is asking about composing command line arguments on the fly using an array of arguments.
bash
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Suppose I have a graphical program named app
. Usage example: app -t 'first tab' -t 'second tab'
opens two 'tabs' in that program.
The question is: how can I execute the command (i.e. app
) from within a bash
script if the number of arguments can vary?
Consider this:
#!/bin/bash
tabs=(
'first tab'
'second tab'
)
# Open the app (starting with some tabs).
appÃÂ # ... How to get `app -t 'first tab' -t 'second tab'`?
I would like the above script to have an effect equivalent to app -t 'first tab' -t 'second tab'
. How can such a bash script be written?
Edit: note that the question is asking about composing command line arguments on the fly using an array of arguments.
bash
Suppose I have a graphical program named app
. Usage example: app -t 'first tab' -t 'second tab'
opens two 'tabs' in that program.
The question is: how can I execute the command (i.e. app
) from within a bash
script if the number of arguments can vary?
Consider this:
#!/bin/bash
tabs=(
'first tab'
'second tab'
)
# Open the app (starting with some tabs).
appÃÂ # ... How to get `app -t 'first tab' -t 'second tab'`?
I would like the above script to have an effect equivalent to app -t 'first tab' -t 'second tab'
. How can such a bash script be written?
Edit: note that the question is asking about composing command line arguments on the fly using an array of arguments.
bash
edited Dec 24 '17 at 4:37
asked Dec 23 '17 at 8:25
Flux
1645
1645
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
5
down vote
accepted
Giving the arguments from an array is easy, "$array[@]"
expands to the array entries as distinct words (arguments). We just need to add the -t
flags. To do that, we can loop over the first array, and build another array for the full list of arguments, adding the -t
flags as we go:
#!/bin/bash
tabs=("first tab" "second tab")
args=()
for t in "$tabs[@]" ; do
args+=(-t "$t")
done
app "$args[@]"
Use "$@"
instead of "$tabs[@]"
to take the command line arguments of the script instead of a hard coded list.
add a comment |Â
up vote
3
down vote
tabs=("-t" "one tab" "-t" "second tab")
echo app "$tabs[@]"
app -t one tab -t second tab
So now you should convert your original array to array with "-t" flags. Hope it's not a problem at all.
add a comment |Â
up vote
0
down vote
It's easier with zsh
:
#!/bin/zsh -
tabs=(
'first tab'
'second tab'
)
app -t$^tabs
That calls app
as if you had entered:
app -t'first tab' -t'second tab'
rc
, es
and fish
do that implicitly (zsh's $^array
is actually inspired from rc
's ^
):
In those shells,
app -t$tabs
would do that as well.
To call app
as if with
app -t 'first tab' -t 'second tab'
as opposed to
app -t'first tab' -t'second tab'
That is where -t
and first tab
are two different arguments to app
, with zsh
:
app $$:--t:^^tabs
using the $array1:^^array2
array-zipping operator where $:--t
(minimal form of $var:-default
) is used to inline the first array.
add a comment |Â
up vote
-1
down vote
Basically the following bash
script will read the file which include sites line by line and open each site in a new tab.
#!/bin/bash
while read line
do
xdg-open "$line"
done < /root/file
Usage
./script.sh sites
Ah but you are running the command (xdg-open
in your case) multiple times. I would like the command to be run only once.
â Flux
Dec 23 '17 at 8:44
The question is about getting the arguments from an array, not a file.
â Barmar
Dec 23 '17 at 11:43
@roaima The question has always been about an array. There has never been any mention of files.
â Flux
Dec 23 '17 at 17:38
@Flux sorry yes. Can't think now why I'd have written that. Deleting...
â roaima
Dec 23 '17 at 18:45
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
Giving the arguments from an array is easy, "$array[@]"
expands to the array entries as distinct words (arguments). We just need to add the -t
flags. To do that, we can loop over the first array, and build another array for the full list of arguments, adding the -t
flags as we go:
#!/bin/bash
tabs=("first tab" "second tab")
args=()
for t in "$tabs[@]" ; do
args+=(-t "$t")
done
app "$args[@]"
Use "$@"
instead of "$tabs[@]"
to take the command line arguments of the script instead of a hard coded list.
add a comment |Â
up vote
5
down vote
accepted
Giving the arguments from an array is easy, "$array[@]"
expands to the array entries as distinct words (arguments). We just need to add the -t
flags. To do that, we can loop over the first array, and build another array for the full list of arguments, adding the -t
flags as we go:
#!/bin/bash
tabs=("first tab" "second tab")
args=()
for t in "$tabs[@]" ; do
args+=(-t "$t")
done
app "$args[@]"
Use "$@"
instead of "$tabs[@]"
to take the command line arguments of the script instead of a hard coded list.
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
Giving the arguments from an array is easy, "$array[@]"
expands to the array entries as distinct words (arguments). We just need to add the -t
flags. To do that, we can loop over the first array, and build another array for the full list of arguments, adding the -t
flags as we go:
#!/bin/bash
tabs=("first tab" "second tab")
args=()
for t in "$tabs[@]" ; do
args+=(-t "$t")
done
app "$args[@]"
Use "$@"
instead of "$tabs[@]"
to take the command line arguments of the script instead of a hard coded list.
Giving the arguments from an array is easy, "$array[@]"
expands to the array entries as distinct words (arguments). We just need to add the -t
flags. To do that, we can loop over the first array, and build another array for the full list of arguments, adding the -t
flags as we go:
#!/bin/bash
tabs=("first tab" "second tab")
args=()
for t in "$tabs[@]" ; do
args+=(-t "$t")
done
app "$args[@]"
Use "$@"
instead of "$tabs[@]"
to take the command line arguments of the script instead of a hard coded list.
answered Dec 23 '17 at 9:15
ilkkachu
49.9k674137
49.9k674137
add a comment |Â
add a comment |Â
up vote
3
down vote
tabs=("-t" "one tab" "-t" "second tab")
echo app "$tabs[@]"
app -t one tab -t second tab
So now you should convert your original array to array with "-t" flags. Hope it's not a problem at all.
add a comment |Â
up vote
3
down vote
tabs=("-t" "one tab" "-t" "second tab")
echo app "$tabs[@]"
app -t one tab -t second tab
So now you should convert your original array to array with "-t" flags. Hope it's not a problem at all.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
tabs=("-t" "one tab" "-t" "second tab")
echo app "$tabs[@]"
app -t one tab -t second tab
So now you should convert your original array to array with "-t" flags. Hope it's not a problem at all.
tabs=("-t" "one tab" "-t" "second tab")
echo app "$tabs[@]"
app -t one tab -t second tab
So now you should convert your original array to array with "-t" flags. Hope it's not a problem at all.
answered Dec 23 '17 at 9:02
Fedor Dikarev
963210
963210
add a comment |Â
add a comment |Â
up vote
0
down vote
It's easier with zsh
:
#!/bin/zsh -
tabs=(
'first tab'
'second tab'
)
app -t$^tabs
That calls app
as if you had entered:
app -t'first tab' -t'second tab'
rc
, es
and fish
do that implicitly (zsh's $^array
is actually inspired from rc
's ^
):
In those shells,
app -t$tabs
would do that as well.
To call app
as if with
app -t 'first tab' -t 'second tab'
as opposed to
app -t'first tab' -t'second tab'
That is where -t
and first tab
are two different arguments to app
, with zsh
:
app $$:--t:^^tabs
using the $array1:^^array2
array-zipping operator where $:--t
(minimal form of $var:-default
) is used to inline the first array.
add a comment |Â
up vote
0
down vote
It's easier with zsh
:
#!/bin/zsh -
tabs=(
'first tab'
'second tab'
)
app -t$^tabs
That calls app
as if you had entered:
app -t'first tab' -t'second tab'
rc
, es
and fish
do that implicitly (zsh's $^array
is actually inspired from rc
's ^
):
In those shells,
app -t$tabs
would do that as well.
To call app
as if with
app -t 'first tab' -t 'second tab'
as opposed to
app -t'first tab' -t'second tab'
That is where -t
and first tab
are two different arguments to app
, with zsh
:
app $$:--t:^^tabs
using the $array1:^^array2
array-zipping operator where $:--t
(minimal form of $var:-default
) is used to inline the first array.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
It's easier with zsh
:
#!/bin/zsh -
tabs=(
'first tab'
'second tab'
)
app -t$^tabs
That calls app
as if you had entered:
app -t'first tab' -t'second tab'
rc
, es
and fish
do that implicitly (zsh's $^array
is actually inspired from rc
's ^
):
In those shells,
app -t$tabs
would do that as well.
To call app
as if with
app -t 'first tab' -t 'second tab'
as opposed to
app -t'first tab' -t'second tab'
That is where -t
and first tab
are two different arguments to app
, with zsh
:
app $$:--t:^^tabs
using the $array1:^^array2
array-zipping operator where $:--t
(minimal form of $var:-default
) is used to inline the first array.
It's easier with zsh
:
#!/bin/zsh -
tabs=(
'first tab'
'second tab'
)
app -t$^tabs
That calls app
as if you had entered:
app -t'first tab' -t'second tab'
rc
, es
and fish
do that implicitly (zsh's $^array
is actually inspired from rc
's ^
):
In those shells,
app -t$tabs
would do that as well.
To call app
as if with
app -t 'first tab' -t 'second tab'
as opposed to
app -t'first tab' -t'second tab'
That is where -t
and first tab
are two different arguments to app
, with zsh
:
app $$:--t:^^tabs
using the $array1:^^array2
array-zipping operator where $:--t
(minimal form of $var:-default
) is used to inline the first array.
answered Dec 23 '17 at 22:56
Stéphane Chazelas
282k53518851
282k53518851
add a comment |Â
add a comment |Â
up vote
-1
down vote
Basically the following bash
script will read the file which include sites line by line and open each site in a new tab.
#!/bin/bash
while read line
do
xdg-open "$line"
done < /root/file
Usage
./script.sh sites
Ah but you are running the command (xdg-open
in your case) multiple times. I would like the command to be run only once.
â Flux
Dec 23 '17 at 8:44
The question is about getting the arguments from an array, not a file.
â Barmar
Dec 23 '17 at 11:43
@roaima The question has always been about an array. There has never been any mention of files.
â Flux
Dec 23 '17 at 17:38
@Flux sorry yes. Can't think now why I'd have written that. Deleting...
â roaima
Dec 23 '17 at 18:45
add a comment |Â
up vote
-1
down vote
Basically the following bash
script will read the file which include sites line by line and open each site in a new tab.
#!/bin/bash
while read line
do
xdg-open "$line"
done < /root/file
Usage
./script.sh sites
Ah but you are running the command (xdg-open
in your case) multiple times. I would like the command to be run only once.
â Flux
Dec 23 '17 at 8:44
The question is about getting the arguments from an array, not a file.
â Barmar
Dec 23 '17 at 11:43
@roaima The question has always been about an array. There has never been any mention of files.
â Flux
Dec 23 '17 at 17:38
@Flux sorry yes. Can't think now why I'd have written that. Deleting...
â roaima
Dec 23 '17 at 18:45
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
Basically the following bash
script will read the file which include sites line by line and open each site in a new tab.
#!/bin/bash
while read line
do
xdg-open "$line"
done < /root/file
Usage
./script.sh sites
Basically the following bash
script will read the file which include sites line by line and open each site in a new tab.
#!/bin/bash
while read line
do
xdg-open "$line"
done < /root/file
Usage
./script.sh sites
edited Dec 23 '17 at 18:46
roaima
39.8k545108
39.8k545108
answered Dec 23 '17 at 8:28
ñÃÂñýàñüÃÂÃÂùcñ÷
407418
407418
Ah but you are running the command (xdg-open
in your case) multiple times. I would like the command to be run only once.
â Flux
Dec 23 '17 at 8:44
The question is about getting the arguments from an array, not a file.
â Barmar
Dec 23 '17 at 11:43
@roaima The question has always been about an array. There has never been any mention of files.
â Flux
Dec 23 '17 at 17:38
@Flux sorry yes. Can't think now why I'd have written that. Deleting...
â roaima
Dec 23 '17 at 18:45
add a comment |Â
Ah but you are running the command (xdg-open
in your case) multiple times. I would like the command to be run only once.
â Flux
Dec 23 '17 at 8:44
The question is about getting the arguments from an array, not a file.
â Barmar
Dec 23 '17 at 11:43
@roaima The question has always been about an array. There has never been any mention of files.
â Flux
Dec 23 '17 at 17:38
@Flux sorry yes. Can't think now why I'd have written that. Deleting...
â roaima
Dec 23 '17 at 18:45
Ah but you are running the command (
xdg-open
in your case) multiple times. I would like the command to be run only once.â Flux
Dec 23 '17 at 8:44
Ah but you are running the command (
xdg-open
in your case) multiple times. I would like the command to be run only once.â Flux
Dec 23 '17 at 8:44
The question is about getting the arguments from an array, not a file.
â Barmar
Dec 23 '17 at 11:43
The question is about getting the arguments from an array, not a file.
â Barmar
Dec 23 '17 at 11:43
@roaima The question has always been about an array. There has never been any mention of files.
â Flux
Dec 23 '17 at 17:38
@roaima The question has always been about an array. There has never been any mention of files.
â Flux
Dec 23 '17 at 17:38
@Flux sorry yes. Can't think now why I'd have written that. Deleting...
â roaima
Dec 23 '17 at 18:45
@Flux sorry yes. Can't think now why I'd have written that. Deleting...
â roaima
Dec 23 '17 at 18:45
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412638%2frun-a-command-using-arguments-that-come-from-an-array%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password