Scripts that take input
When you run ./backup.sh notes.txt, the script receives notes.txt as an argument, exactly like the arguments you've been giving ls and grep since lesson 1-3. Inside the script:
$1,$2,$3… are the first, second, third arguments$#is how many arguments there are$0is the script's own name"$@"is all the arguments at once (you'll loop over it in unit 9)
To experiment without creating a file each time, bash has a trick: set -- Ada Grace fills $1 and $2 as if the script had been called with those arguments.
Code exercise · bash
Run it. set -- simulates being called with two arguments, then we read them back.
Code exercise · bash
The real thing: a one-line backup.sh that uses $1, called twice with different arguments (bash script.sh args is the run-without-chmod form from lesson 8-1).
Quiz
A script is run as ./ship.sh box1 box2 box3. Inside it, what is $# and what is $2?
Code exercise · bash
Your turn. Write greet.sh (one echo line) so that `bash greet.sh Ada` prints `Hello, Ada! You are guest number 1`, using $1 and $#. Then call it twice as shown. Expected output: ``` Hello, Ada! You are guest number 1 Hello, Grace! You are guest number 2 ```