Linux sh for syntax

Shell script «for» loop syntax

How can I get the compiler to realize it should treat $max as the other end of the array, and not part of a string?

what system and shell are you using? What kind of goofy system has sh or bash, but doesn’t have seq, a coreutil?

Small style nit: I usually see the do and then keywords on the same line as for and if , respectively. E.g., for i in <2..10>; do

11 Answers 11

Brace expansion, is performed before other expansions, so you cannot use that for variable length sequences.

Instead, use the seq 2 $max method as user mob stated.

So, for your example it would be:

max=10 for i in `seq 2 $max` do echo "$i" done 

There is no good reason to use an external command such as seq to count and increment numbers in the for loop, hence it is recommend that you avoid using seq . This command is left for compatibility with old bash. The built-in commands are fast enough. for (( EXP1; EXP2; EXP3 )) .

@miller the for (( . )) syntax isn’t POSIX and that means for example that it won’t work out of the box on things like Alpine Linux. seq does.

Try the arithmetic-expression version of for :

This is available in most versions of bash, and should be Bourne shell (sh) compatible also.

@Flow: Hm, I just tried it on a couple of systems (Linux and BSD based) with #!/bin/sh and it worked fine. Invoked under bash and specifically under /bin/sh, still worked. Maybe the version of sh matters? Are you on some old Unix?

Читайте также:  Linux mint boot in console

Very few systems have a dedicated sh , instead making it a link to other another shell. Ideally, such a shell invoked as sh would only support those features in the POSIX standard, but by default let some of their extra features through. The C-style for-loop is not a POSIX feature, but may be in sh mode by the actual shell.

Источник

syntax of for loop in linux shell scripting

I don’t get the same error as you for that — I get unexpected end of file. Anyway, you have a missing semicolon.

For bash the syntax seems fine. Can you try to make sure you’re running the right shell ? ls -l /bin/bash , maybe ?

4 Answers 4

You probably run it with sh , not bash . Try bash test1.sh , or ./test1.sh if it’s executable, but not sh test1.sh .

A standard POSIX shell only accepts the syntax for varname in list

The C-like for-loop syntax for (( expr1; expr2; expr3 )) is a bashism.

You can get similar behavior in the standard POSIX shell using for c in $(seq 1 5)

Make sh a symbolic link to bash and then you can do sh ./test1.sh

Your shell script (as shown) runs in both Korn shell and Bash. Some thoughts:

  • You might need a space after the shebang (#! /bin/bash and not #!/bin/bash). However, Dennis Ritchie had originally specified the space is optional. Besides, it isn’t the error you get with Bourne shell (you get syntax error: ‘(‘ unexpected instead).
  • Are you on a Windows system? Just a stab in the dark. This doesn’t look like a Windows error.
  • Is this Solaris or HP/UX system? They might not be running true versions of Bash, or maybe an older version. However, even the oldest version of Bash recognizes the for ((x;y;z)) construct.
#! /bin/bash set -vx echo "Random = $RANDOM" #Test for bash/Kornshell. Will be blank in other shells echo \$BASH_VERSINFO[0] = $ #Should only work in BASH echo \$BASH_VERSINFO[1] = $ echo \$BASH_VERSINFO[2] = $ echo \$BASH_VERSINFO[3] = $ echo \$BASH_VERSINFO[4] = $ echo \$BASH_VERSINFO[5] = $ for ((c=0, c 
  • The set -xv will display all lines as they are executed.
  • The $RANDOM should display a value if this is either BASH or Kornshell (your for loop will work in either one).
  • The should only be set if this is truly BASH. These aren't even set even if you run Korn shell after you're in BASH (unlike $SHELL which will still contain bash ).

If the for loop still gives you trouble, just delete it. Somewhere in this script, we'll find out if you're really executing a bash shell or not.

Источник

Оцените статью
Adblock
detector