0

Following is the shell script which iterates over command line arguments and prints the values

for var in "$@"
do
    echo $var
done

Now if i want to iterate from the second command line argument (the first argument being used for some other purpose), what is the command to exclude the first argument alone in iteration ?

3 Answers 3

1

Use shift:

#!/usr/bin/env bash

shift
for var in "$@"; do
    echo "$var"
done
Sign up to request clarification or add additional context in comments.

3 Comments

I am trying to initialise an array in shell script like this testArr=( "A" "B" ). But I get the following error ' shell1.sh: Syntax error: "(" unexpected'. Can you please tell me what the reason for this error is?
Do you have spaces somewhere? Anyway, please open a new question.
in "$@" is useless since it's the default. You can just use: for var; do echo "$var"; done
1

The first argument is in $1, so doing

var1=$1

will store it into var1

You can then use shift to "delete" the first arg and still use your for loop:

~$cat s.sh
var1=$1
shift
echo var1=$var1
for var in "$@"
do
    echo $var
done
~$ ./s.sh 1 2 3
var1=1
2
3

From man bash:

shift [n]

The positional parameters from n+1 ... are renamed to $1 .... Parameters represented by the numbers $# down to $#-n+1 are unset. n must be a non-negative number less than or equal to $#. If n is 0, no parameters are changed. If n is not given, it is assumed to be 1. If n is greater than $#, the positional parameters are not changed. The return status is greater than zero if n is greater than $# or less than zero; otherwise 0.

3 Comments

I am trying to initialise an array in shell script like this testArr=( "A" "B" ). But I get the following error ' shell1.sh: Syntax error: "(" unexpected'. Can you please tell me what the reason for this error is?
That's a different question. Did you check stackoverflow.com/questions/16486879/… or tldp.org/LDP/abs/html/arrays.html ?
fredtantini: Those seem to be different questions. I am find able to find the reason for the error in initialising array
0

This example behaves more like a standard unix command line utility. Options are processed in any order and can have modifiers. It does not work for every situation (like modifiers with spaces in them), but I've found it to be very useful for scripts that I want to have some default behavior, but occasionally I want to modify one or more parameters.

#!/bin/bash

#defaults
VAR1="Yours"
VAR2="Mine"
VAR3="Ours"
SHARENICE="false"

while [ $# -gt 0 ]; do
    case "$1" in
        #single parameter
        -s)  SHARENICE="true"
        shift
        ;;
        #modified parameters
        -1)  VAR1="$2"
        shift 2
        ;;
        -2)  VAR2="$2"
        shift 2
        ;;
        -3)  VAR3="$2"
        shift 2
        ;;
        #you could put a 'usage' function here,
        #or if your last parameter has no modifier,
        #like a mandatory input, it will now be at
        #$1
        *)
        break
        ;;
    esac
done

echo -n "$VAR1, $VAR2 and $VAR3.  "
if [ "$SHARENICE" != "true" ]; then
    echo "Too bad we don't get along."
else
    echo "Good thing we play nice!"
fi

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.