Here's what I would do :
#!/bin/bash
image_name=${1-1}
wget "http://www.example.com/images/$image_name.jpg"
The script is to be called as getimage x
. If x
is omitted, 1
will be chosen as a default value.
The first line is a shebang : it tells your shell to execute the script with the specified executable, /bin/bash
.
The second line defines an image_name
variable and assigns it the result of an expression. That expression is a parameter expansion, which will return $1
(that is the first parameter passed to the script) if it is defined, or 1
either. It's hard to see in this case, but the syntax is ${variable-default}
where variable
is a variable name whose value you want, and default
a default used when that variable isn't defined.
The third line is your command, where the static 1
has been replaced by a reference to the image_name
variable. Note that ""
have been added in case the image_name
contains spaces : that would break the url in two parts that would be understood by wget
as two separate parameters. Don't use ''
though, the $variable
contained in the string wouldn't be expanded to their value.