#!/bin/sh
for i in {1..5}
do
echo "Welcome"
done
Would work, displays Welcome 5 times.
#!/bin/sh
howmany=`grep -c $1 /root/file`
for i in {1..$howmany}
do
echo "Welcome"
done
Doesn't work! howmany
would equal 5 as that is what the output of grep -c
would display. $1 is parameter 1 which is specific when running the script.
Any ideas?
If the intent is merely to iterate over numbers in a range - as in the OP's case - the best choice is not to use brace expansion, but instead use bash's C-style loop - see user000001's answer.
If use of brace expansion is desired nonetheless:
If you do NOT need the numbers in the list to have a prefix or postfix, use the seq
utility with an unquoted command substitution (small caveat: seq
is NOT a POSIX utility, but it is widely available); e.g.
echo $(seq 3)
-> 1 2 3
; start number 1
implied
echo $(seq -f '%02.f' 3)
-> 01 02 03
- zero-paddedecho $(seq 2 4)
-> 2 3 4
; explicit start and end numbersecho $(seq 1 2 5)
-> 1 3 5
; custom increment (the 2
in the middle)If you DO need the numbers in the list to have a prefix or postfix, you have several choices:
seq
utility with its -f
option for providing a printf
-style format string (as used above for zero-padding), or pure Bash workarounds based on eval
(extra care needed!) or building an array in a loop, all of which are detailed in this answer.awk
or perl
.eval
with variables driving a sequence brace expression:The variables are validated beforehand, to make sure they contain decimal integers.
from=1 to=3 # sample values
# Ensure that $from and $to are decimal numbers and abort, if they are not.
(( 10#$from + 10#$to || 1 )) 2>/dev/null || { echo "Need decimal integers" >&2; exit 1; }
eval echo "A{$from..$to}" # -> 'A1 A2 A3'
The main purpose of brace expansion is to expand to a list of tokens with each token having an optional prefix and/or postfix; brace expansions must be unquoted and come in 2 flavors:
echo A{b,c,d}
-> Ab Ac Ad
, i.e., 3 tokens, as implied by the number of args.echo {/,$HOME/}Library
e.g., -> /Library /User/jdoe/Library
a sequence expression (range) with ..
, typically numerical - variables NOT supported
{a..c}
{1..10}
, {10..1}
, {-1..2}
A{1..3}#
-> A1# A2# A3#
{$from..$to} # !! FAILS
- $from
and $to
are interpreted as literals and therefore not recognized as either a single letter or a decimal integer - no brace expansion is performed (see below).
zsh
and ksh
. echo A{1..5..2}
-> A1 A3 A5
- numbers incremented by 2echo A{001..003}
-> A001 A002 A003
An invalid brace expression is not expanded (treated like a regular unquoted string, with {
and }
treated as literals):
echo {}
-> '{}'
- invalid as a brace expr.: at least 2 ,
-separated tokens needed
{}
with find
, for instance.echo {1..$to}
-> '{1..<value-of-$to>}'
- invalid as a brace expr. in bash
: variables not supported; however, valid in ksh
and zsh
.fish
, by contrast, expands any {...}
sequence; similarly, zsh
has option BRACE_CCL
(OFF by default) for expanding individual characters inside {..}
, which effectively causes expansion of any nonempty {...}
sequence.)