How to pass arguments to a bash script

You can simply pass arguments to a bash script by typing the name of the script followed by arguments separated by space.

# $1 for first argument
# $2 for second argument and so on
# For arg number greater than 9 wrap it around with curly brackets e.g. ${10}

#This will print first two arguments.
echo $1 $2


[[email protected] ~]# ./sample 1 2
1 2

If you need pass string (containing spaces) as an argument, wrap it around in quotes e.g.
./sample "My First Argument" "SecondArg"

Leave a Reply

Your email address will not be published. Required fields are marked *