0

I'm trying to tell a shell script information this way (when I execute it via SSH):

bash myscript.sh 'input1' 'input2' 'input3'

However I've no idea on how to convert the inputs 1, 2 and 3 into variables in shell script, like $var1, $var2 and $var3.

Anybody know how?

1 Answer 1

3

Its very simple, the arguments passed are held in their respective numbers:

input1 would be on $1
input2 would be on $2
input3 would be on $3

And if I recall it correctly $0 is the script path/filename

And you can for example do this:

#!/bin/sh

echo "arg1='$1' - arg2='$2' - arg3='$3'"
var1="$1"
var2="$2"
var3="$3"
echo "var1='$var1' - var2='$var2' - var3='$var3'"

Not really needed but just to illustrate.

Sign up to request clarification or add additional context in comments.

5 Comments

Does it need to be like $var1="$1" $var2="$2" $var3="$3"?
@user2248259 you can directly use $1 instead of assigning it to a variable.
I know, but I need to convert $1 to $var1
@user2248259 then you have to use var1="$1" just like I did on the example above.
Shell is not Perl; the dollar sign is only needed to expand the variable to its value, not to assign to it.

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.