Friday, 19th June 2026 - 14:40 UTC+0100** --- ## How can a file be edited? Quite easily, as there are multiple text editors available on Linux, with one of the most used ones being Nano. If you were to type `nano file.ext`, it would open `file.ext` for editing or it would create it if it didn't already exist. It has its own shortcuts too: - `Ctrl+K` deletes line. - `Ctrl+U` un-deletes a line - `Ctrl+O` saves the files, though you will need to press `Enter` to confirm the filename - `Ctrl+X` exits the editor. However, if you were doing a complex analysis that happens to involve keeping a records of the commands used, opening Nano and writing them by memory is quite an inefficient way of doing it. A better way would be to run a command like this that outputs history, pipes it so it shows only the most recent, and then redirect it to a file. Something like: ```bash history | tail -n 10 > figure-5.history ``` Or in a different scenario: ```bash history | tail -n 3 > steps.txt ``` ## But what if I wanted to save commands so I could re-run them later instead of trying to recall them? Well, you could write the commands in `file.sh` and execute it with: ```bash bash file.sh ``` ## How can I re-use pipes? Well, it's a good thing scripts can contain pipes as well. For example, the file `all-dates.sh` could contain the following: ```bash cut -d , -f 1 seasonal/*.csv | grep -v Date | sort | uniq ``` meaning that this command: ```bash bash all-dates.sh > dates.out ``` will extract the unique dates from the `.csv` files and save them in `dates.out` ## And if I wanted to pass filenames into scripts? Luckily, there's a way of processing any file you want by way of a script, using the special expression `$@`, which basically means "all of the CLI parameters given to the script." For example, if `unique-lines.sh` contained `sort $@ | uniq` when this command is run: ```bash bash unique-lines.sh seasonal/summer.csv seasonal/winter.csv ``` The shell would replace `$@` with `seasonal/summer.csv seasonal/winter.csv` and process both the files. `$1`, `$2`, and so on can also be used to refer to specific CLI parameters. For example, a script, `column.sh`, that selects a single column from a `.csv` file provided by the user as the first parameter and a column as the second: ```bash cut -d , -f $2 $1 ``` It could be run using: ```bash bash column.sh seasonal/autumn.csv 1 ``` ## How can one shell script do many things? It is possible to have one script contain numerous lines of commands. A script could tell you how many records are in the shortest and longest of a data file specified. In nano, `Ctrl+K` can be used to cut a line, and if you press `Ctrl+U` twice, two copies of the line that was just cut will be pasted. ## And if I wanted to write loops into a shell script? Well, it's a good thing Shell scripts can contain loops, and can be written using semi-colons if choosing not to split them across lines without the use of semi-colons. It all comes down to readability. An example script would look a bit like ```bash # Print the first and last data records of each file. for filename in $@ do head -n 2 $filename | tail -n 1 tail -n 1 $filename done ``` The first line of the script is a comment that serves as a brief explanation. Comments in bash always start with `#`. ## What if filenames are not provided? Putting filenames in the wrong place within a shell script is quite a common mistake. A command in which this happens would not do much, as `head` would be waiting forever and a day: ```bash head -n 5 | tail -n 3 file.ext ``` --- [[Upskilling|← Back to Upskilling]]