BASH shell question, variable sustitution $1... command line args
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Looks like you are trying to use $COUNT as an index to the passed parameters. I don't think it can work that way. Instead try indirect expansion.
Code:
foo$ cat echoparms
#! /bin/bash
c=0
while [[ c -le $# ]]
do
c=$((c+1))
echo ${!c}
done
foo$ ./echoparms one two three
one
two
three
foo$
Of course you could always just copy all the arguments into a true Bash array first.
If I were to write a shell script to sum all arguments it would look a little different. I think what you're doing may work okay for you, but it could be simpler.
HTH
Edit
This question reminds me of some of my school assignments. For extra credit, can you tell why my loop runs one more iteration than needed?
You don't put $ in front of variables inside arithmetic expressions.
Maybe you don't, but Bash allows it and expressions evaluate just the same.
Code:
foo$ i=1
foo$ echo $(( i ))
1
foo$ echo $(( $i ))
1
foo$ if (( $i == 1 )) ; then echo "true" ; else echo "false" ; fi
true
foo$ i=0
foo$ if (( $i == 1 )) ; then echo "true" ; else echo "false" ; fi
false
foo$
Quote:
Originally Posted by 6.5 Shell Arithmetic - Bash Reference Manual
Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax.
Maybe you don't, but Bash allows it and expressions evaluate just the same.[/code]
OK, I thought it wouldn't work if you used the "$" syntax.
I also didn't know that for if statements, you can omit the "$" from in front of "((" and it will evaluate if 1 is returned. I always did it like this:
I also didn't know that for if statements, you can omit the "$" from in front of "((" and it will evaluate if 1 is returned.
Quote:
Originally Posted by 3.2.4.2 Conditional Constructs - Bash Reference Manual
((...))
(( expression ))
The arithmetic expression is evaluated according to the rules described below (see Shell Arithmetic). If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.