How do I pass arguments to shell script?

23,117

Solution 1

For bash (which is one shell, but probably the most common in the Linux world), the equivalent is:

java temptable $1 $2

assuming there's no spaces in the arguments. If there are spaces, you should quote your arguments:

java temptable "$1" "$2"

You can also do:

java temptable $*

or:

java temptable "$@"

if you want all parameters passed through (again, that second one is equivalent to quoting each of the parameters: "$1" "$2" "$3" ...).

Solution 2

#!/bin/bash
# Call this script with at least 3 parameters, for example
# sh scriptname 1 2 3
echo "first parameter is $1"
echo "Second parameter is $2"
echo "Third parameter is $3"
exit 0

Output:

[root@localhost ~]# sh parameters.sh 47 9 34
first parameter is 47
Second parameter is 9
Third parameter is 34
Share:
23,117
Admin
Author by

Admin

Updated on July 23, 2022

Comments

  • Admin
    Admin almost 2 years

    I have a batch file like this:

    java temptable %1 %2
    

    I need the equivalent shell script for the above. I will pass the arguments to the shell script and that should be passed to temptable.