Skip to content

Commit 64932c2

Browse files
committed
[linux] notes on bash positional parameters
1 parent def6e9d commit 64932c2

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

docs/dev-notes/linux/bash.md

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ else
481481
fi
482482
```
483483
484-
### Arguments
484+
## Positional Parameters
485485
486486
|Expression|Description|
487487
|----------|-----------|
@@ -492,8 +492,46 @@ fi
492492
|`$_`|Last argument of the previous command|
493493
494494
>
495-
> \[!note\] `$@` and `$*` must be quoted in order
496-
> Otherwise, they do exactly the same thing (arguments as separate strings)
495+
> \[!note\] `$@` and `$*` must be quoted; unquoted behavior is exactly the same i.e. args as separate strings
496+
497+
### `$@` vs `$*` Example
498+
499+
```bash
500+
print_params() {
501+
echo "\$1 = $1"
502+
echo "\$2 = $2"
503+
echo "\$3 = $3"
504+
echo "\$4 = $4"
505+
}
506+
pass_params() {
507+
echo -e "\n" '$* =>'; print_params $*
508+
echo -e "\n" '"$*" =>'; print_params "$*"
509+
echo -e "\n" '$@ =>'; print_params $@
510+
echo -e "\n" '"$@" =>'; print_params "$@"
511+
}
512+
513+
pass_params "word" "words with spaces"
514+
$* =>
515+
$1 = word
516+
$2 = words
517+
$3 = with
518+
$4 = spaces
519+
"$*" =>
520+
$1 = word words with spaces
521+
$2 =
522+
$3 =
523+
$4 =
524+
$@ =>
525+
$1 = word
526+
$2 = words
527+
$3 = with
528+
$4 = spaces
529+
"$@" =>
530+
$1 = word
531+
$2 = words with spaces
532+
$3 =
533+
$4 =
534+
```
497535
498536
### Special Parameters
499537

0 commit comments

Comments
 (0)