1

So this is my first function:

#!/bin/bash

lets_print () {

    echo hello $1
}

lets_print Earth

lets_print Mars

ready to show result

user@bash$ ./demo.sh

Hello Earth

Hello Mars

And this is my 2nd function:

#!/bin/bash

lets_print () {

    echo hello $1 $2
}

lets_print Earth

lets_print Mars

ready to show the second result:

user@bash$ ./demo.sh

Hello Earth

Hello Mars

Can someone please explain why they have the same result?

Right now I am thinking $1=Earth and $2=Mars. But I know this is wrong.

1 Answer 1

4

They are showing the same result because you are only passing one positional parameter per function call. In order for mars to be the second parameter you would need to call like this:

lets_print Earth Mars

Recommended reading on positional parameters: 3.4.1 Positional Parameters

2

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.