|
Command-line arguments make scripts flexible and reusable. Instead of editing a script to change a filename or value, you can pass different values when running it: ./backup.sh /home/user /backups ./backup.sh /var/log /backups The same script works with different inputs. For these
examples, we use |
|
|
Output:
Arguments set to: hello world foo bar |
|
|
The shell provides special variables for accessing arguments:
Assigning positional parameters to named variables makes your code more readable and self-documenting. |
|
|
Output:
Script name ($0): /script Positional parameters: First argument ($1): hello Second argument ($2): world Third argument ($3): foo Argument count ($#): 4 Named variables: input=hello, output=world |
|
|
When you need all arguments at once, use
Use |
|
|
Output:
With $@ (separate words in a for loop): - apple - banana - cherry With $* (single string): apple banana cherry |
|
|
Quoting matters when arguments contain spaces. Always use
|
|
|
Output:
With quoted "$@" (correct): Received 2 arguments: 'hello world' 'foo bar' With quoted "$*" (becomes one argument): [hello world foo bar] With unquoted $@ (breaks on spaces - wrong): Received 4 arguments: 'hello' 'world' 'foo' 'bar' Rule: Always use "$@" when passing arguments. |
|
|
The
|
|
|
Output:
Before shift: $1=first $2=second $3=third $4=fourth ($#=4) After shift: $1=second $2=third $3=fourth ($#=3) After shift 2: $1=fourth ($#=1) |
|
|
A common pattern combines |
|
|
Output:
Processing files: Processing: file1.txt Processing: file2.txt Processing: file3.txt All files processed (remaining args: 0) |
|
|
Scripts should validate that they received the expected
arguments. Use
|
|
|
Output:
Testing check_any_args: Error: No arguments provided Usage: command <arg>... Got 2 argument(s): one two Testing check_exactly_two: Error: Expected exactly 2 arguments, got 1 Usage: command <source> <destination> Got exactly two: one and two |
|
|
Default values for missing arguments:
|
|
|
Output:
Hello, Guest name is still: '' Hello, Guest name is now: 'Guest' Name: customvalue, Port: 8080 |
|
|
The The |
|
|
Output:
With both arguments: Source: input.txt Destination: output.txt |
|
|
For arguments beyond This rarely comes up since most scripts don’t take 10+ positional arguments, but it’s important to know. |
|
|
Output:
Without braces (wrong):
$10 = a0 (actually $1 + '0')
With braces (correct):
${10} = j
${11} = k
${12} = l
|
|
|
Bash
Bash provides additional syntax for working with arguments:
These are useful when you need to skip the first few arguments or access the last one directly. Note: These features are Bash-specific and not available in POSIX sh. |
|
|
Output:
All arguments: one two three four five
Last argument (${!#}): five
Skip first (${@:2}): two three four five
Args 2-3 (${@:2:2}): two three
|
|
|
This pattern combines everything into a complete argument parser. The while/case loop handles flags and options:
|
|
|
Output:
Verbose mode enabled Files to process: file1.txt file2.txt |
|