Linux Commands | Shell Script | How to Use Shell Arguments and Parameters

Let’s look at how to pass parameters in a shell script.

  • $#: Returns the number of parameters.
  • $number: Returns the parameter at the specified position.
  • $@: Returns all parameters.

Example

First, create a file.

$ vi cmd.sh

Write the following content.

#!/bin/bash

echo "Parameter count: $#"
echo "First parameter: $1"
echo "Second parameter: $2"
echo "All parameter contents: $@"

After writing the content, grant execute permission to the file.

$ chmod 755 cmd.sh 

Execution Result:

$ ./cmd.sh hello devkuma
Parameter count: 2
First parameter: hello
Second parameter: devkuma
All parameter contents: hello devkuma

Application

Tomcat restart shell script

kill -9 $(ps aux |awk '/tomcat/ {print $2}')
sleep 2
/home/ubuntu/tomcat/bin/startup.sh