0

Consider I have script like given below.

demo.sh:

name=$1 age=$2 echo $name echo $age 

when I execute the script like

sh demo.sh ARUN 24 

Output is:

ARUN 24 

But, When I execute the script like

sh -c demo.sh ARUN 24 

Output is nothing.

I know if i use sh -c arguments will be assigned starting from $0. But only file name is getting assigned to $0. How can i assign more than one parameters when is use sh -c? Please explain me how it works.

1 Answer 1

1

Use this syntax instead:

sh -c "demo.sh ARUN 24" 

In your case those two arguments where passed to the sh command, not the demo.sh script. Put them in quotes to pass them trought sh to the script.

Note the easiest way would be to make the script executable and add a hashbang (#!) line to it. The scripts would then look as follows:

#!/bin/sh name=$1 age=$2 echo $name echo $age 

Make it executable with:

chmod +x demo.sh 

Then run it as follows (without the interpreter sh, the interpreter is now gathered from the #! line):

./demo.sh ARUN 24 
Sign up to request clarification or add additional context in comments.

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.