Linux for Developers Course
Linux for Developers
/
Intermediate

Command Line Arguments

Definition

Input parameters passed directly into a script from the terminal when the script is executed, allowing the script to perform dynamically based on the user's input.

Explain Like I'm New

Handing the script instructions before it runs. Instead of hardcoding 'Bob' into the script, you run the script and hand it 'Bob' from the outside.

Real World Example

You write a script `create_user.sh`. Instead of hardcoding the username, you run `./create_user.sh alice`. Inside the script, the variable `$1` magically contains the word 'alice', and the script creates her account.

Common Use Cases

  • •Dynamic scripting
  • •Creating custom terminal tools

Interview Questions

basic

  • In a script, what does the special variable `$1` represent?

intermediate

  • What does the special variable `$#` represent in a script?

Flash Cards

Question

What is $1?

Click to reveal answer
Answer

It is the very first argument provided to the script by the user.

Question

What is $#?

Click to reveal answer
Answer

It is a count of exactly how many arguments the user provided. It is heavily used in `if` statements to throw an error if the user forgets to provide a required argument (e.g., `if [ $# -eq 0 ]; then echo "You forgot the argument!"; fi`).