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?

2
  • echo $i just output 1 2 3, but my expectation is -a -b -c. Commented Nov 25, 2016 at 11:03
  • Actually, I want to use echo $($i) to achieve the same result with echo $1 $2 $3, but I find echo $($i) is an invalid command. Commented Nov 25, 2016 at 11:07

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

1 Comment

a shell expert, this resolve my issue, thank you very much.
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.

1 Comment

So he is trying to run 1, 2 and 3 as commands, which are not. Nicely explained.
0

You could also use a one-line script

#!/bin/bash
echo $*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.