Using a text editor to create your script
Scripts are saved to text files. You can create one in a text editor, like
$ nano yourscript.sh
The filename in general and the filename extension in particular does not matter. In your script file, you have to declare the executable of the interpreter to run it (a shebang). Then add your code. To save your script in
nano, press Ctrl+X, then type
y and hit Enter.
Composing your script
If you pass arguments to the script, you can reference them in your code by number (the first passed argument is
$1, the second one is
$2, and so on).
$0 is the script's name itself.
As a general rule, always use double quotes around variables.
Taking your code, for example, the script might be:
curl -O "$1"
head -n 12 cities.csv
There can be a space after
#!, but it is not necessary. In most cases, you can check
$0 variable in terminal for the shell executable interpreting your commands in terminal:
$ echo $0
Improving your code
Assuming that all you want are the first 12 lines of the downloaded file, you can skip saving the downloaded file to disk and pipe it to the next command:
curl -s "$1" | head -n 12
As a bonus, it allows you to omit the filename in your script.
Running the script
Assuming you save your script as
yourscript.sh, add permission to execute it first:
$ chmod +x yourscript.sh
Then, to run it, you need to specify the path to it and provide the arguments. If it is in the current directory, you can specify the path to it by prepending
$ ./yourscript.sh 'https://people.sc.fsu.edu/~jburkardt/data/csv/cities.csv'
#!/bin/shatop the file, and apply
chmod +x file.shto make it executable.Call it like