Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arguments passed into for loop in bash script [duplicate]

Tags:

I am trying to pass the argument as max limit for the for loop like this:

#!/bin/bash  for i in {1..$1} do     echo $i done 

This however returns {1..2} when called with argument 2, instead of executing the script and giving me

1 2 
like image 746
Milo Wielondek Avatar asked Jan 21 '11 22:01

Milo Wielondek


People also ask

What is $@ in bash script?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

How do I pass arguments from one bash script to another?

Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth. The variable $0 references to the current script.

What is the difference between && and || in bash?

Save this answer. Show activity on this post. && and || are boolean operators. && is the logical AND operator, and bash uses short cut evaluation, so the second command is only executed if there is the chance that the whole expression can be true.

What does || in bash do?

Just like && , || is a bash control operator: && means execute the statement which follows only if the preceding statement executed successfully (returned exit code zero). || means execute the statement which follows only if the preceding statement failed (returned a non-zero exit code).


2 Answers

Variable substitutions are not done inside of curly braces. You can use fixed numbers but not variables.

Brace Expansion

A sequence expression takes the form {x..y}, where x and y are either integers or single characters. ...

Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result. It is strictly textual. Bash does not apply any syntactic interpretation to the context of the expansion or the text between the braces.

A correctly-formed brace expansion must contain unquoted opening and closing braces, and at least one unquoted comma or a valid sequence expression. Any incorrectly formed brace expansion is left unchanged.

Try one of these alternatives:

for ((i = 1; i <= $1; i++)); do     echo $i done  # Not recommended with large sequences. for i in $(seq 1 $1); do     echo $i done 
like image 163
John Kugelman Avatar answered Oct 13 '22 22:10

John Kugelman


This will cycle through all true arguments (a.k.a. "testo mesto" is one argument)

#cycle through all args for (( i=1; i<=$1; i++ )); do     eval arg=\$$i     echo "$arg" done 

OR

#cycle through all args for (( i=1; i<=$1; i++ )); do     echo "${!i}" done 
like image 44
Daniel K Avatar answered Oct 13 '22 20:10

Daniel K