0PricingLogin
Linux Command Line & Bash Scripting Mastery · Lesson

Script Arguments and Options ($@, $#, getopts)

Learn to pass arguments to your scripts and parse command-line options using special variables and 'getopts'.

Script Arguments: Why Use Them?

Ever used a command like ls -l or cp file1 file2? The -l, file1, and file2 are all arguments! They tell the command what to do or what to act upon.

In Bash scripting, arguments make your scripts flexible. Instead of hardcoding values, your script can accept input directly from the command line when it's run.

Accessing Positional Arguments

When you run a script with arguments, Bash automatically assigns them to special variables called positional parameters.

  • The first argument is $1.
  • The second argument is $2.
  • And so on, up to $9.

For arguments beyond 9, you'd use curly braces like ${10}, ${11}, etc. These variables hold the values passed to your script.

All lessons in this course

  1. Looping Constructs (for, while)
  2. Functions in Bash Scripts
  3. Script Arguments and Options ($@, $#, getopts)
  4. The case Statement & Pattern Matching
← Back to Linux Command Line & Bash Scripting Mastery