简体   繁体   中英

How do I pass arguments to shell script?

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.

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" ...).

#!/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM