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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.