Skip to content
Related Articles

Related Articles

Bash shell script to find out the largest value from given command line arguments

Improve Article
Save Article
Like Article
  • Last Updated : 05 Sep, 2019

Write a shell script to find out the largest value from the given number of command-line arguments.


Special variables in bash:

$@- All arguments.
$#- Number of arguments.
$0- Filename.
$1, $2, $3, $4 ... - Specific arguments.


  • If the number of arguments is 0, end the program.
  • If not zero, then
    • Initialize a variable maxEle with first argument.
    • Loop over all the arguments. Compare each argument with maxEle and update it if the argument is greater.

#Check if the number of arguments passed is zero
if [ "$#" = 0 ]
    #Script exits if no
    #arguments passed
    echo "No arguments passed."
    exit 1
#Initialize maxEle with 
#the first argument
#Loop that compares maxEle with the 
#passed arguments and updates it
for arg in "$@"
    if [ "$arg" -gt "$maxEle" ]
echo "Largest value among the arguments passed is: $maxEle"
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!