Iterate through command line arguments in Bash
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
Can someone explain the difference between these two code blocks? I would think Block #2 would output the same as Block #1 but it does not. Can someone explain why?
# ./arguments.sh hello my name is X
Block #1
for i
do
echo $i
done
Output:
hello
my
name
is
X
Block #2
args=$#
for (( i=1; i<=$args; i+=1 ))
do
echo $i
done
Output:
1
2
3
4
5
bash
add a comment |Â
up vote
1
down vote
favorite
Can someone explain the difference between these two code blocks? I would think Block #2 would output the same as Block #1 but it does not. Can someone explain why?
# ./arguments.sh hello my name is X
Block #1
for i
do
echo $i
done
Output:
hello
my
name
is
X
Block #2
args=$#
for (( i=1; i<=$args; i+=1 ))
do
echo $i
done
Output:
1
2
3
4
5
bash
2
Replace in second exampleecho $i
byecho $i $1; shift
to get more confused or enlightened.
â Cyrus
Jan 2 '16 at 21:31
@Cyrus Yes, I also foundwhile (( "$#" )); do echo $1; shift; done
It served its purpose; confusion. I would still like to know why Block #2 does not produce the same output as Block #1 :)
â jes516
Jan 2 '16 at 21:42
1
@jes516 in #2, you are explicitly creating a variablei
and assigning an integer index to it; in #1, the shell is implicitly assigning each positional parameter in turn toi
. See thefor name [ [ in [ word ... ] ] ; ] do list ; done
construct in theCompound commands
section ofman bash
.
â steeldriver
Jan 2 '16 at 22:19
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Can someone explain the difference between these two code blocks? I would think Block #2 would output the same as Block #1 but it does not. Can someone explain why?
# ./arguments.sh hello my name is X
Block #1
for i
do
echo $i
done
Output:
hello
my
name
is
X
Block #2
args=$#
for (( i=1; i<=$args; i+=1 ))
do
echo $i
done
Output:
1
2
3
4
5
bash
Can someone explain the difference between these two code blocks? I would think Block #2 would output the same as Block #1 but it does not. Can someone explain why?
# ./arguments.sh hello my name is X
Block #1
for i
do
echo $i
done
Output:
hello
my
name
is
X
Block #2
args=$#
for (( i=1; i<=$args; i+=1 ))
do
echo $i
done
Output:
1
2
3
4
5
bash
bash
edited Aug 31 at 16:44
Rui F Ribeiro
36.8k1272117
36.8k1272117
asked Jan 2 '16 at 21:17
jes516
3231620
3231620
2
Replace in second exampleecho $i
byecho $i $1; shift
to get more confused or enlightened.
â Cyrus
Jan 2 '16 at 21:31
@Cyrus Yes, I also foundwhile (( "$#" )); do echo $1; shift; done
It served its purpose; confusion. I would still like to know why Block #2 does not produce the same output as Block #1 :)
â jes516
Jan 2 '16 at 21:42
1
@jes516 in #2, you are explicitly creating a variablei
and assigning an integer index to it; in #1, the shell is implicitly assigning each positional parameter in turn toi
. See thefor name [ [ in [ word ... ] ] ; ] do list ; done
construct in theCompound commands
section ofman bash
.
â steeldriver
Jan 2 '16 at 22:19
add a comment |Â
2
Replace in second exampleecho $i
byecho $i $1; shift
to get more confused or enlightened.
â Cyrus
Jan 2 '16 at 21:31
@Cyrus Yes, I also foundwhile (( "$#" )); do echo $1; shift; done
It served its purpose; confusion. I would still like to know why Block #2 does not produce the same output as Block #1 :)
â jes516
Jan 2 '16 at 21:42
1
@jes516 in #2, you are explicitly creating a variablei
and assigning an integer index to it; in #1, the shell is implicitly assigning each positional parameter in turn toi
. See thefor name [ [ in [ word ... ] ] ; ] do list ; done
construct in theCompound commands
section ofman bash
.
â steeldriver
Jan 2 '16 at 22:19
2
2
Replace in second example
echo $i
by echo $i $1; shift
to get more confused or enlightened.â Cyrus
Jan 2 '16 at 21:31
Replace in second example
echo $i
by echo $i $1; shift
to get more confused or enlightened.â Cyrus
Jan 2 '16 at 21:31
@Cyrus Yes, I also found
while (( "$#" )); do echo $1; shift; done
It served its purpose; confusion. I would still like to know why Block #2 does not produce the same output as Block #1 :)â jes516
Jan 2 '16 at 21:42
@Cyrus Yes, I also found
while (( "$#" )); do echo $1; shift; done
It served its purpose; confusion. I would still like to know why Block #2 does not produce the same output as Block #1 :)â jes516
Jan 2 '16 at 21:42
1
1
@jes516 in #2, you are explicitly creating a variable
i
and assigning an integer index to it; in #1, the shell is implicitly assigning each positional parameter in turn to i
. See the for name [ [ in [ word ... ] ] ; ] do list ; done
construct in the Compound commands
section of man bash
.â steeldriver
Jan 2 '16 at 22:19
@jes516 in #2, you are explicitly creating a variable
i
and assigning an integer index to it; in #1, the shell is implicitly assigning each positional parameter in turn to i
. See the for name [ [ in [ word ... ] ] ; ] do list ; done
construct in the Compound commands
section of man bash
.â steeldriver
Jan 2 '16 at 22:19
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
roaima's answer answers the question that you actually asked:
Q: What is the difference between these two code blocks?ÃÂ
Why do they give different output?
A: The first loop is iterating over the command line arguments;
the second one is iterating over the argument numbers (indices).
... although I presume that you would have figured that much out
for yourself in another six to eight minutes â it's kind-of obvious.
You probably want the second program to do something like
echo $$i # This doesn't do what you want.
to display the argument that is indexed by the number
that is stored in variable i
(and referenced asÃÂ $i
).ÃÂ
As noted in the comment, that doesn't do what you want.ÃÂ
(It does do something;
I encourage you to experiment and figure out what it does.)ÃÂ
But this is close to something that does work:
eval echo $$i # Don't do this.
or, equivalently,
eval echo '$'"$i" # Don't do this.
These commands
- get the value of
i
(one of the numbers 1, 2, 3, ...) - stick a
$
in front of it, forming$1
,$2
,$3
, etc. - use the
eval
command to say, "take this command line
that I've just constructed, and evaluate it as if I had typed it.
So that would have the effect of executing
echo $1
echo $2
echo $3
ï¸Â
But, as the comments suggest, you should try to avoid this.ÃÂ eval
can be dangerous if the input is anything other than plain words.ÃÂ
Search this site; you'll find plenty of explanations of that.
But there is a fairly safe way to get the second program
to do the same thing the first one does: change
echo $i
to
echo $!i
OK, first of all, $i
is pretty much the same as $i
.ÃÂ
The !
gives you an effect similar to that of the eval
command âÂÂ$!x
looks up the value of x
(i.e., $x
or $x
)
and uses that as the name of the variable to look up.ÃÂ
So, if x=foo
, then $!x
is the same as $foo
.ÃÂ
The above code does the same thing with i
,
fetching the parameter whose name is the value of i
.
By the way, you should always quote all references to shell variables
(e.g., "$i"
, "$#"
, "$args"
, "$i"
and "$!i"
)
unless you have a good reason not to,
and youâÂÂre sure you know what youâÂÂre doing.
Just so I am clear, bash is looking for the variable$1
,$2
, etc... where the dollar sign is part of the variable name. For example I did,args="$#"; a="$1"; b="$2"; echo "$a"; echo "$b"
and that worked. I thought bash was looking for the variable1
,2
, etc... where the dollar sign was part of the syntax (for lack of better terms).
â jes516
Jan 3 '16 at 4:31
I'd say you were right the first time.â If you have a variable nameda
, at the risk of being redundant, its name isa
.â You set it by sayinga=â¦
; e.g.,a=aardvark
ora="$1"
.â You access it (e.g., in anecho
â¯command) with"$a"
or"$a"
, so the$
and$â¦
are part of the syntax (and thatâÂÂs a perfectly appropriate term).âÂÂ$1
,$2
, etc⦠are slightly different from$a
,$b
, etcâ¦, but not much.
â G-Man
Jan 3 '16 at 22:33
Perfect, I appreciate the insight. While I have your attention (if you don't mind) why doesfor i; do echo $i; done
work as well to print arguments? Does bash automatically run that code block for each argument? I am used to seeingfor i in list; ...
â jes516
Jan 4 '16 at 3:08
You understandfor i in "$1" "$2" "$3" "$4" "$5"; do â¦
, right?â But the problem is that you donâÂÂt know in advance how many arguments there are (i.e., how many there are going to be).â roaimaâÂÂs answer brushed up against this without delving into it.âÂÂ$@
(which, above all other$
expressions, should always be quoted) is a magic token that expands to the list of arguments, however long or short that is.âÂÂ(You can, and should, read about this, the$â¦
syntax, and other things in sh/bash documentation;â â¦ (ContâÂÂd)
â G-Man
Jan 4 '16 at 6:39
(ContâÂÂd) â¦â e.g., what you get if you typeman sh
orman bash
(if that doesnâÂÂt work for you, try this), the Bash Reference Manual, the POSIX specification for Shell Command Language, and other references.)â Andfor i; do â¦
is a special shorthand forfor i in "$@"; do â¦
â which is equivalent tofor i in "$1" "$2" "$3" "$4" "$5"; do â¦
(if you have five arguments).
â G-Man
Jan 4 '16 at 6:41
add a comment |Â
up vote
4
down vote
The first block iterates (implicitly) across the command line arguments "$@"
for i in "$@" # same as your "for i"
do
echo "$i"
done
The second block iterates explicitly across the number of arguments, printing the index as it goes:
args=$# # number of command line args
for (( i=1; i<=$args; i+=1 )) # loop from 1 to N (where N is number of args)
do
echo $i
done
Given that, as per your example, $#
is 5, then the $i
variable will take the values 1
, 2
, 3
, 4
, 5
.
As pointed out in another (now deleted) answer, you can reference the command line arguments by index like this:
args=$#
for (( i=1; i<=$args; i++ ))
do
echo "$i - $!i"
done
Just to expand on my comment below,echo "$i - $!i"
is echoing the variablei
- the variable$i
, wherei
is the number and$i
evaluates to the "shell variable"$1
..
â jes516
Jan 3 '16 at 4:34
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
roaima's answer answers the question that you actually asked:
Q: What is the difference between these two code blocks?ÃÂ
Why do they give different output?
A: The first loop is iterating over the command line arguments;
the second one is iterating over the argument numbers (indices).
... although I presume that you would have figured that much out
for yourself in another six to eight minutes â it's kind-of obvious.
You probably want the second program to do something like
echo $$i # This doesn't do what you want.
to display the argument that is indexed by the number
that is stored in variable i
(and referenced asÃÂ $i
).ÃÂ
As noted in the comment, that doesn't do what you want.ÃÂ
(It does do something;
I encourage you to experiment and figure out what it does.)ÃÂ
But this is close to something that does work:
eval echo $$i # Don't do this.
or, equivalently,
eval echo '$'"$i" # Don't do this.
These commands
- get the value of
i
(one of the numbers 1, 2, 3, ...) - stick a
$
in front of it, forming$1
,$2
,$3
, etc. - use the
eval
command to say, "take this command line
that I've just constructed, and evaluate it as if I had typed it.
So that would have the effect of executing
echo $1
echo $2
echo $3
ï¸Â
But, as the comments suggest, you should try to avoid this.ÃÂ eval
can be dangerous if the input is anything other than plain words.ÃÂ
Search this site; you'll find plenty of explanations of that.
But there is a fairly safe way to get the second program
to do the same thing the first one does: change
echo $i
to
echo $!i
OK, first of all, $i
is pretty much the same as $i
.ÃÂ
The !
gives you an effect similar to that of the eval
command âÂÂ$!x
looks up the value of x
(i.e., $x
or $x
)
and uses that as the name of the variable to look up.ÃÂ
So, if x=foo
, then $!x
is the same as $foo
.ÃÂ
The above code does the same thing with i
,
fetching the parameter whose name is the value of i
.
By the way, you should always quote all references to shell variables
(e.g., "$i"
, "$#"
, "$args"
, "$i"
and "$!i"
)
unless you have a good reason not to,
and youâÂÂre sure you know what youâÂÂre doing.
Just so I am clear, bash is looking for the variable$1
,$2
, etc... where the dollar sign is part of the variable name. For example I did,args="$#"; a="$1"; b="$2"; echo "$a"; echo "$b"
and that worked. I thought bash was looking for the variable1
,2
, etc... where the dollar sign was part of the syntax (for lack of better terms).
â jes516
Jan 3 '16 at 4:31
I'd say you were right the first time.â If you have a variable nameda
, at the risk of being redundant, its name isa
.â You set it by sayinga=â¦
; e.g.,a=aardvark
ora="$1"
.â You access it (e.g., in anecho
â¯command) with"$a"
or"$a"
, so the$
and$â¦
are part of the syntax (and thatâÂÂs a perfectly appropriate term).âÂÂ$1
,$2
, etc⦠are slightly different from$a
,$b
, etcâ¦, but not much.
â G-Man
Jan 3 '16 at 22:33
Perfect, I appreciate the insight. While I have your attention (if you don't mind) why doesfor i; do echo $i; done
work as well to print arguments? Does bash automatically run that code block for each argument? I am used to seeingfor i in list; ...
â jes516
Jan 4 '16 at 3:08
You understandfor i in "$1" "$2" "$3" "$4" "$5"; do â¦
, right?â But the problem is that you donâÂÂt know in advance how many arguments there are (i.e., how many there are going to be).â roaimaâÂÂs answer brushed up against this without delving into it.âÂÂ$@
(which, above all other$
expressions, should always be quoted) is a magic token that expands to the list of arguments, however long or short that is.âÂÂ(You can, and should, read about this, the$â¦
syntax, and other things in sh/bash documentation;â â¦ (ContâÂÂd)
â G-Man
Jan 4 '16 at 6:39
(ContâÂÂd) â¦â e.g., what you get if you typeman sh
orman bash
(if that doesnâÂÂt work for you, try this), the Bash Reference Manual, the POSIX specification for Shell Command Language, and other references.)â Andfor i; do â¦
is a special shorthand forfor i in "$@"; do â¦
â which is equivalent tofor i in "$1" "$2" "$3" "$4" "$5"; do â¦
(if you have five arguments).
â G-Man
Jan 4 '16 at 6:41
add a comment |Â
up vote
3
down vote
accepted
roaima's answer answers the question that you actually asked:
Q: What is the difference between these two code blocks?ÃÂ
Why do they give different output?
A: The first loop is iterating over the command line arguments;
the second one is iterating over the argument numbers (indices).
... although I presume that you would have figured that much out
for yourself in another six to eight minutes â it's kind-of obvious.
You probably want the second program to do something like
echo $$i # This doesn't do what you want.
to display the argument that is indexed by the number
that is stored in variable i
(and referenced asÃÂ $i
).ÃÂ
As noted in the comment, that doesn't do what you want.ÃÂ
(It does do something;
I encourage you to experiment and figure out what it does.)ÃÂ
But this is close to something that does work:
eval echo $$i # Don't do this.
or, equivalently,
eval echo '$'"$i" # Don't do this.
These commands
- get the value of
i
(one of the numbers 1, 2, 3, ...) - stick a
$
in front of it, forming$1
,$2
,$3
, etc. - use the
eval
command to say, "take this command line
that I've just constructed, and evaluate it as if I had typed it.
So that would have the effect of executing
echo $1
echo $2
echo $3
ï¸Â
But, as the comments suggest, you should try to avoid this.ÃÂ eval
can be dangerous if the input is anything other than plain words.ÃÂ
Search this site; you'll find plenty of explanations of that.
But there is a fairly safe way to get the second program
to do the same thing the first one does: change
echo $i
to
echo $!i
OK, first of all, $i
is pretty much the same as $i
.ÃÂ
The !
gives you an effect similar to that of the eval
command âÂÂ$!x
looks up the value of x
(i.e., $x
or $x
)
and uses that as the name of the variable to look up.ÃÂ
So, if x=foo
, then $!x
is the same as $foo
.ÃÂ
The above code does the same thing with i
,
fetching the parameter whose name is the value of i
.
By the way, you should always quote all references to shell variables
(e.g., "$i"
, "$#"
, "$args"
, "$i"
and "$!i"
)
unless you have a good reason not to,
and youâÂÂre sure you know what youâÂÂre doing.
Just so I am clear, bash is looking for the variable$1
,$2
, etc... where the dollar sign is part of the variable name. For example I did,args="$#"; a="$1"; b="$2"; echo "$a"; echo "$b"
and that worked. I thought bash was looking for the variable1
,2
, etc... where the dollar sign was part of the syntax (for lack of better terms).
â jes516
Jan 3 '16 at 4:31
I'd say you were right the first time.â If you have a variable nameda
, at the risk of being redundant, its name isa
.â You set it by sayinga=â¦
; e.g.,a=aardvark
ora="$1"
.â You access it (e.g., in anecho
â¯command) with"$a"
or"$a"
, so the$
and$â¦
are part of the syntax (and thatâÂÂs a perfectly appropriate term).âÂÂ$1
,$2
, etc⦠are slightly different from$a
,$b
, etcâ¦, but not much.
â G-Man
Jan 3 '16 at 22:33
Perfect, I appreciate the insight. While I have your attention (if you don't mind) why doesfor i; do echo $i; done
work as well to print arguments? Does bash automatically run that code block for each argument? I am used to seeingfor i in list; ...
â jes516
Jan 4 '16 at 3:08
You understandfor i in "$1" "$2" "$3" "$4" "$5"; do â¦
, right?â But the problem is that you donâÂÂt know in advance how many arguments there are (i.e., how many there are going to be).â roaimaâÂÂs answer brushed up against this without delving into it.âÂÂ$@
(which, above all other$
expressions, should always be quoted) is a magic token that expands to the list of arguments, however long or short that is.âÂÂ(You can, and should, read about this, the$â¦
syntax, and other things in sh/bash documentation;â â¦ (ContâÂÂd)
â G-Man
Jan 4 '16 at 6:39
(ContâÂÂd) â¦â e.g., what you get if you typeman sh
orman bash
(if that doesnâÂÂt work for you, try this), the Bash Reference Manual, the POSIX specification for Shell Command Language, and other references.)â Andfor i; do â¦
is a special shorthand forfor i in "$@"; do â¦
â which is equivalent tofor i in "$1" "$2" "$3" "$4" "$5"; do â¦
(if you have five arguments).
â G-Man
Jan 4 '16 at 6:41
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
roaima's answer answers the question that you actually asked:
Q: What is the difference between these two code blocks?ÃÂ
Why do they give different output?
A: The first loop is iterating over the command line arguments;
the second one is iterating over the argument numbers (indices).
... although I presume that you would have figured that much out
for yourself in another six to eight minutes â it's kind-of obvious.
You probably want the second program to do something like
echo $$i # This doesn't do what you want.
to display the argument that is indexed by the number
that is stored in variable i
(and referenced asÃÂ $i
).ÃÂ
As noted in the comment, that doesn't do what you want.ÃÂ
(It does do something;
I encourage you to experiment and figure out what it does.)ÃÂ
But this is close to something that does work:
eval echo $$i # Don't do this.
or, equivalently,
eval echo '$'"$i" # Don't do this.
These commands
- get the value of
i
(one of the numbers 1, 2, 3, ...) - stick a
$
in front of it, forming$1
,$2
,$3
, etc. - use the
eval
command to say, "take this command line
that I've just constructed, and evaluate it as if I had typed it.
So that would have the effect of executing
echo $1
echo $2
echo $3
ï¸Â
But, as the comments suggest, you should try to avoid this.ÃÂ eval
can be dangerous if the input is anything other than plain words.ÃÂ
Search this site; you'll find plenty of explanations of that.
But there is a fairly safe way to get the second program
to do the same thing the first one does: change
echo $i
to
echo $!i
OK, first of all, $i
is pretty much the same as $i
.ÃÂ
The !
gives you an effect similar to that of the eval
command âÂÂ$!x
looks up the value of x
(i.e., $x
or $x
)
and uses that as the name of the variable to look up.ÃÂ
So, if x=foo
, then $!x
is the same as $foo
.ÃÂ
The above code does the same thing with i
,
fetching the parameter whose name is the value of i
.
By the way, you should always quote all references to shell variables
(e.g., "$i"
, "$#"
, "$args"
, "$i"
and "$!i"
)
unless you have a good reason not to,
and youâÂÂre sure you know what youâÂÂre doing.
roaima's answer answers the question that you actually asked:
Q: What is the difference between these two code blocks?ÃÂ
Why do they give different output?
A: The first loop is iterating over the command line arguments;
the second one is iterating over the argument numbers (indices).
... although I presume that you would have figured that much out
for yourself in another six to eight minutes â it's kind-of obvious.
You probably want the second program to do something like
echo $$i # This doesn't do what you want.
to display the argument that is indexed by the number
that is stored in variable i
(and referenced asÃÂ $i
).ÃÂ
As noted in the comment, that doesn't do what you want.ÃÂ
(It does do something;
I encourage you to experiment and figure out what it does.)ÃÂ
But this is close to something that does work:
eval echo $$i # Don't do this.
or, equivalently,
eval echo '$'"$i" # Don't do this.
These commands
- get the value of
i
(one of the numbers 1, 2, 3, ...) - stick a
$
in front of it, forming$1
,$2
,$3
, etc. - use the
eval
command to say, "take this command line
that I've just constructed, and evaluate it as if I had typed it.
So that would have the effect of executing
echo $1
echo $2
echo $3
ï¸Â
But, as the comments suggest, you should try to avoid this.ÃÂ eval
can be dangerous if the input is anything other than plain words.ÃÂ
Search this site; you'll find plenty of explanations of that.
But there is a fairly safe way to get the second program
to do the same thing the first one does: change
echo $i
to
echo $!i
OK, first of all, $i
is pretty much the same as $i
.ÃÂ
The !
gives you an effect similar to that of the eval
command âÂÂ$!x
looks up the value of x
(i.e., $x
or $x
)
and uses that as the name of the variable to look up.ÃÂ
So, if x=foo
, then $!x
is the same as $foo
.ÃÂ
The above code does the same thing with i
,
fetching the parameter whose name is the value of i
.
By the way, you should always quote all references to shell variables
(e.g., "$i"
, "$#"
, "$args"
, "$i"
and "$!i"
)
unless you have a good reason not to,
and youâÂÂre sure you know what youâÂÂre doing.
edited Apr 13 '17 at 12:36
Communityâ¦
1
1
answered Jan 2 '16 at 23:09
G-Man
11.8k92658
11.8k92658
Just so I am clear, bash is looking for the variable$1
,$2
, etc... where the dollar sign is part of the variable name. For example I did,args="$#"; a="$1"; b="$2"; echo "$a"; echo "$b"
and that worked. I thought bash was looking for the variable1
,2
, etc... where the dollar sign was part of the syntax (for lack of better terms).
â jes516
Jan 3 '16 at 4:31
I'd say you were right the first time.â If you have a variable nameda
, at the risk of being redundant, its name isa
.â You set it by sayinga=â¦
; e.g.,a=aardvark
ora="$1"
.â You access it (e.g., in anecho
â¯command) with"$a"
or"$a"
, so the$
and$â¦
are part of the syntax (and thatâÂÂs a perfectly appropriate term).âÂÂ$1
,$2
, etc⦠are slightly different from$a
,$b
, etcâ¦, but not much.
â G-Man
Jan 3 '16 at 22:33
Perfect, I appreciate the insight. While I have your attention (if you don't mind) why doesfor i; do echo $i; done
work as well to print arguments? Does bash automatically run that code block for each argument? I am used to seeingfor i in list; ...
â jes516
Jan 4 '16 at 3:08
You understandfor i in "$1" "$2" "$3" "$4" "$5"; do â¦
, right?â But the problem is that you donâÂÂt know in advance how many arguments there are (i.e., how many there are going to be).â roaimaâÂÂs answer brushed up against this without delving into it.âÂÂ$@
(which, above all other$
expressions, should always be quoted) is a magic token that expands to the list of arguments, however long or short that is.âÂÂ(You can, and should, read about this, the$â¦
syntax, and other things in sh/bash documentation;â â¦ (ContâÂÂd)
â G-Man
Jan 4 '16 at 6:39
(ContâÂÂd) â¦â e.g., what you get if you typeman sh
orman bash
(if that doesnâÂÂt work for you, try this), the Bash Reference Manual, the POSIX specification for Shell Command Language, and other references.)â Andfor i; do â¦
is a special shorthand forfor i in "$@"; do â¦
â which is equivalent tofor i in "$1" "$2" "$3" "$4" "$5"; do â¦
(if you have five arguments).
â G-Man
Jan 4 '16 at 6:41
add a comment |Â
Just so I am clear, bash is looking for the variable$1
,$2
, etc... where the dollar sign is part of the variable name. For example I did,args="$#"; a="$1"; b="$2"; echo "$a"; echo "$b"
and that worked. I thought bash was looking for the variable1
,2
, etc... where the dollar sign was part of the syntax (for lack of better terms).
â jes516
Jan 3 '16 at 4:31
I'd say you were right the first time.â If you have a variable nameda
, at the risk of being redundant, its name isa
.â You set it by sayinga=â¦
; e.g.,a=aardvark
ora="$1"
.â You access it (e.g., in anecho
â¯command) with"$a"
or"$a"
, so the$
and$â¦
are part of the syntax (and thatâÂÂs a perfectly appropriate term).âÂÂ$1
,$2
, etc⦠are slightly different from$a
,$b
, etcâ¦, but not much.
â G-Man
Jan 3 '16 at 22:33
Perfect, I appreciate the insight. While I have your attention (if you don't mind) why doesfor i; do echo $i; done
work as well to print arguments? Does bash automatically run that code block for each argument? I am used to seeingfor i in list; ...
â jes516
Jan 4 '16 at 3:08
You understandfor i in "$1" "$2" "$3" "$4" "$5"; do â¦
, right?â But the problem is that you donâÂÂt know in advance how many arguments there are (i.e., how many there are going to be).â roaimaâÂÂs answer brushed up against this without delving into it.âÂÂ$@
(which, above all other$
expressions, should always be quoted) is a magic token that expands to the list of arguments, however long or short that is.âÂÂ(You can, and should, read about this, the$â¦
syntax, and other things in sh/bash documentation;â â¦ (ContâÂÂd)
â G-Man
Jan 4 '16 at 6:39
(ContâÂÂd) â¦â e.g., what you get if you typeman sh
orman bash
(if that doesnâÂÂt work for you, try this), the Bash Reference Manual, the POSIX specification for Shell Command Language, and other references.)â Andfor i; do â¦
is a special shorthand forfor i in "$@"; do â¦
â which is equivalent tofor i in "$1" "$2" "$3" "$4" "$5"; do â¦
(if you have five arguments).
â G-Man
Jan 4 '16 at 6:41
Just so I am clear, bash is looking for the variable
$1
, $2
, etc... where the dollar sign is part of the variable name. For example I did, args="$#"; a="$1"; b="$2"; echo "$a"; echo "$b"
and that worked. I thought bash was looking for the variable 1
, 2
, etc... where the dollar sign was part of the syntax (for lack of better terms).â jes516
Jan 3 '16 at 4:31
Just so I am clear, bash is looking for the variable
$1
, $2
, etc... where the dollar sign is part of the variable name. For example I did, args="$#"; a="$1"; b="$2"; echo "$a"; echo "$b"
and that worked. I thought bash was looking for the variable 1
, 2
, etc... where the dollar sign was part of the syntax (for lack of better terms).â jes516
Jan 3 '16 at 4:31
I'd say you were right the first time.â If you have a variable named
a
, at the risk of being redundant, its name is a
.â You set it by saying a=â¦
; e.g., a=aardvark
or a="$1"
.â You access it (e.g., in an echo
â¯command) with "$a"
or "$a"
, so the $
and $â¦
are part of the syntax (and thatâÂÂs a perfectly appropriate term).â $1
, $2
, etc⦠are slightly different from $a
, $b
, etcâ¦, but not much.â G-Man
Jan 3 '16 at 22:33
I'd say you were right the first time.â If you have a variable named
a
, at the risk of being redundant, its name is a
.â You set it by saying a=â¦
; e.g., a=aardvark
or a="$1"
.â You access it (e.g., in an echo
â¯command) with "$a"
or "$a"
, so the $
and $â¦
are part of the syntax (and thatâÂÂs a perfectly appropriate term).â $1
, $2
, etc⦠are slightly different from $a
, $b
, etcâ¦, but not much.â G-Man
Jan 3 '16 at 22:33
Perfect, I appreciate the insight. While I have your attention (if you don't mind) why does
for i; do echo $i; done
work as well to print arguments? Does bash automatically run that code block for each argument? I am used to seeing for i in list; ...
â jes516
Jan 4 '16 at 3:08
Perfect, I appreciate the insight. While I have your attention (if you don't mind) why does
for i; do echo $i; done
work as well to print arguments? Does bash automatically run that code block for each argument? I am used to seeing for i in list; ...
â jes516
Jan 4 '16 at 3:08
You understand
for i in "$1" "$2" "$3" "$4" "$5"; do â¦
, right?â But the problem is that you donâÂÂt know in advance how many arguments there are (i.e., how many there are going to be).â roaimaâÂÂs answer brushed up against this without delving into it.â $@
(which, above all other $
expressions, should always be quoted) is a magic token that expands to the list of arguments, however long or short that is.âÂÂ(You can, and should, read about this, the $â¦
syntax, and other things in sh/bash documentation;â â¦ (ContâÂÂd)â G-Man
Jan 4 '16 at 6:39
You understand
for i in "$1" "$2" "$3" "$4" "$5"; do â¦
, right?â But the problem is that you donâÂÂt know in advance how many arguments there are (i.e., how many there are going to be).â roaimaâÂÂs answer brushed up against this without delving into it.â $@
(which, above all other $
expressions, should always be quoted) is a magic token that expands to the list of arguments, however long or short that is.âÂÂ(You can, and should, read about this, the $â¦
syntax, and other things in sh/bash documentation;â â¦ (ContâÂÂd)â G-Man
Jan 4 '16 at 6:39
(ContâÂÂd) â¦â e.g., what you get if you type
man sh
or man bash
(if that doesnâÂÂt work for you, try this), the Bash Reference Manual, the POSIX specification for Shell Command Language, and other references.)â And for i; do â¦
is a special shorthand for for i in "$@"; do â¦
â which is equivalent to for i in "$1" "$2" "$3" "$4" "$5"; do â¦
(if you have five arguments).â G-Man
Jan 4 '16 at 6:41
(ContâÂÂd) â¦â e.g., what you get if you type
man sh
or man bash
(if that doesnâÂÂt work for you, try this), the Bash Reference Manual, the POSIX specification for Shell Command Language, and other references.)â And for i; do â¦
is a special shorthand for for i in "$@"; do â¦
â which is equivalent to for i in "$1" "$2" "$3" "$4" "$5"; do â¦
(if you have five arguments).â G-Man
Jan 4 '16 at 6:41
add a comment |Â
up vote
4
down vote
The first block iterates (implicitly) across the command line arguments "$@"
for i in "$@" # same as your "for i"
do
echo "$i"
done
The second block iterates explicitly across the number of arguments, printing the index as it goes:
args=$# # number of command line args
for (( i=1; i<=$args; i+=1 )) # loop from 1 to N (where N is number of args)
do
echo $i
done
Given that, as per your example, $#
is 5, then the $i
variable will take the values 1
, 2
, 3
, 4
, 5
.
As pointed out in another (now deleted) answer, you can reference the command line arguments by index like this:
args=$#
for (( i=1; i<=$args; i++ ))
do
echo "$i - $!i"
done
Just to expand on my comment below,echo "$i - $!i"
is echoing the variablei
- the variable$i
, wherei
is the number and$i
evaluates to the "shell variable"$1
..
â jes516
Jan 3 '16 at 4:34
add a comment |Â
up vote
4
down vote
The first block iterates (implicitly) across the command line arguments "$@"
for i in "$@" # same as your "for i"
do
echo "$i"
done
The second block iterates explicitly across the number of arguments, printing the index as it goes:
args=$# # number of command line args
for (( i=1; i<=$args; i+=1 )) # loop from 1 to N (where N is number of args)
do
echo $i
done
Given that, as per your example, $#
is 5, then the $i
variable will take the values 1
, 2
, 3
, 4
, 5
.
As pointed out in another (now deleted) answer, you can reference the command line arguments by index like this:
args=$#
for (( i=1; i<=$args; i++ ))
do
echo "$i - $!i"
done
Just to expand on my comment below,echo "$i - $!i"
is echoing the variablei
- the variable$i
, wherei
is the number and$i
evaluates to the "shell variable"$1
..
â jes516
Jan 3 '16 at 4:34
add a comment |Â
up vote
4
down vote
up vote
4
down vote
The first block iterates (implicitly) across the command line arguments "$@"
for i in "$@" # same as your "for i"
do
echo "$i"
done
The second block iterates explicitly across the number of arguments, printing the index as it goes:
args=$# # number of command line args
for (( i=1; i<=$args; i+=1 )) # loop from 1 to N (where N is number of args)
do
echo $i
done
Given that, as per your example, $#
is 5, then the $i
variable will take the values 1
, 2
, 3
, 4
, 5
.
As pointed out in another (now deleted) answer, you can reference the command line arguments by index like this:
args=$#
for (( i=1; i<=$args; i++ ))
do
echo "$i - $!i"
done
The first block iterates (implicitly) across the command line arguments "$@"
for i in "$@" # same as your "for i"
do
echo "$i"
done
The second block iterates explicitly across the number of arguments, printing the index as it goes:
args=$# # number of command line args
for (( i=1; i<=$args; i+=1 )) # loop from 1 to N (where N is number of args)
do
echo $i
done
Given that, as per your example, $#
is 5, then the $i
variable will take the values 1
, 2
, 3
, 4
, 5
.
As pointed out in another (now deleted) answer, you can reference the command line arguments by index like this:
args=$#
for (( i=1; i<=$args; i++ ))
do
echo "$i - $!i"
done
edited Jan 2 '16 at 23:30
answered Jan 2 '16 at 22:33
roaima
40.5k547110
40.5k547110
Just to expand on my comment below,echo "$i - $!i"
is echoing the variablei
- the variable$i
, wherei
is the number and$i
evaluates to the "shell variable"$1
..
â jes516
Jan 3 '16 at 4:34
add a comment |Â
Just to expand on my comment below,echo "$i - $!i"
is echoing the variablei
- the variable$i
, wherei
is the number and$i
evaluates to the "shell variable"$1
..
â jes516
Jan 3 '16 at 4:34
Just to expand on my comment below,
echo "$i - $!i"
is echoing the variable i
- the variable $i
, where i
is the number and $i
evaluates to the "shell variable" $1
..â jes516
Jan 3 '16 at 4:34
Just to expand on my comment below,
echo "$i - $!i"
is echoing the variable i
- the variable $i
, where i
is the number and $i
evaluates to the "shell variable" $1
..â jes516
Jan 3 '16 at 4:34
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%2f252900%2fiterate-through-command-line-arguments-in-bash%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
2
Replace in second example
echo $i
byecho $i $1; shift
to get more confused or enlightened.â Cyrus
Jan 2 '16 at 21:31
@Cyrus Yes, I also found
while (( "$#" )); do echo $1; shift; done
It served its purpose; confusion. I would still like to know why Block #2 does not produce the same output as Block #1 :)â jes516
Jan 2 '16 at 21:42
1
@jes516 in #2, you are explicitly creating a variable
i
and assigning an integer index to it; in #1, the shell is implicitly assigning each positional parameter in turn toi
. See thefor name [ [ in [ word ... ] ] ; ] do list ; done
construct in theCompound commands
section ofman bash
.â steeldriver
Jan 2 '16 at 22:19