Find the answer to your Linux question:
Results 1 to 2 of 2
I'm a noob a bash scripting and want to write a script where the number of arguments varies. Is there any easy way to do this? The arguments will later ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2011
    Posts
    1

    Bash scripting: Arguments to script


    I'm a noob a bash scripting and want to write a script where the number of arguments varies.

    Is there any easy way to do this?

    The arguments will later be used in the script and should have individual names (but that shouldn't be a problem).

    Furthermore is there a simple way to count the number of arguments? I could of course make a for loop or something, but it would be great if there was an actual function.

    Thank you!

  2. #2
    Linux Newbie
    Join Date
    Apr 2010
    Location
    Novosibirsk, Russia
    Posts
    145

    Post

    A special variable $# always contains the number of arguments, passed to a script. So

    Code:
    #!/bin/bash
    
    echo $#
    would count them and display a result.

    You can access these arguments using $1, $2, $3, $4, etc. variables

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •