Some scripts take any number of arguments, like cp and rm. Two variables help:
| Variable | Holds |
|---|---|
$# | The number of arguments |
"$@" | All of the arguments, each one as one word |
$#: how many?
bash
#!/bin/bash
echo "You gave $# files."text
learner@tidepool:~$ ./backup.sh a.txt b.txt c.txt
You gave 3 files."$@": pass them all on
"$@" gives every argument to a command, each one kept whole:
bash
#!/bin/bash
# Usage: backup.sh FILE...
mkdir -p ~/backup
cp "$@" ~/backup
echo "Copied $# files to ~/backup"cp "$@" ~/backupin the script
cp "week 1.txt" menu.txt ~/backupcp gets 3 arguments
Always put $@ in double quotes. Without them, week 1.txt becomes two words, week and 1.txt.
... in the usage comment means "can repeat", the same as in --help.