1

My shell name is test.sh, and I want to output all parameters which are passed to test.sh , but I find my code can't work well.

#!/bin/bash
i=1
num=$#
while [ $i -le $num ]; do
    echo $($i)
    ((i++))
done

When I run ./test.sh -a -b -c, my expected output is:

-a
-b
-c

but, it tells me

./test.sh: line 5: 1: command not found
./test.sh: line 5: 2: command not found
./test.sh: line 5: 3: command not found

How can I resolve this issue and output all parameters using echo command?

fedorqui
  • 275,237
  • 103
  • 548
  • 598

3 Answers3

3

You are looking for variable indirection:

#!/bin/bash
i=1
num=$#
while [ $i -le $num ]; do
    echo ${!i}         # <--- print the content of $1, $2...
    ((i++))
done

Upon execution, this returns:

$ bash test.sh -a -b "-c d"
-a
-b
-c d

From Bash Reference Manual → 3.5.3 Shell Parameter Expansion:

If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of variable indirection. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. If parameter is a nameref, this expands to the name of the variable referenced by parameter instead of performing the complete indirect expansion. The exceptions to this are the expansions of ${!prefix*} and ${!name[@]} described below. The exclamation point must immediately follow the left brace in order to introduce indirection.

If you wish to make it more verbose, show the mapping of ${1..n} to its value:

#!/bin/bash
i=1
num=$#
while [ $i -le $num ]; do
    printf "$%s = %s\n" "$i" "${!i}"
    ((i++))
done

See the output:

$ bash test.sh -a -b "-c d"
$1 = -a
$2 = -b
$3 = -c d
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
1

In Bash the sequence $(...) is to create a sub-shell to execute a command. It's a "new" way of using backticks.

The expression

$($i)

is equivalent to

`$i`

So what you are doing is calling the arguments as a command in a sub-shell.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

You could also use a one-line script

#!/bin/bash
echo $*
user2314737
  • 27,088
  • 20
  • 102
  • 114