*Thursday, 18th June 2026 - 13:57 UTC+0100** --- The shell stores information in a series of variables, and are always available. Conventionally, these are written in upper case, and a few of the more commonly used ones are: | Variable | Purpose | Value | | :------: | :------ | :---- | | `HOME` | User's home directory | `/home/user` | | `PWD` | Present working directory | Same as `pwd` command | | `SHELL` | Which shell program is being used | `/bin/bash` | | `USER` | User's ID | `user` | A complete list can be outputted to the terminal with the `set` command. Like other Linux commands, it can be combined with other commands using a pipe. For example: ```bash set | grep HISTFILESIZE ``` This outputs only `HISTFILESIZE` and its value. `HISTFILESIZE` determines how many old commands are stored in the command history. By default, the number is 2,000. --- --- There is another kind of variable - a shell variable. It is a bit like a local variable within a programming language. Creating a shall variable is quire easy, as all it requires is assigning a value to a name: ```bash training=seasonal/summer.csv ``` It is important that this is done **without** any spaces before or after the `=` sign. Once this is done, the variable's value can be checked: ```bash echo $training seasonal/summer.csv ``` Shell commands can also be used in loops, which are used to repeat commands many times. For example, if the following command is run: ```echo for filetype in gif, jpg, png; do echo $filetype; done ``` It produces: ```echo gif jpg png ``` The following can be noticed about the loop: 1. The structure is `for` ...variable... `in` ...list... `; do` ..body... `; done` 2. The list of things the loop is to process (in our case, the words `gif`, `jpg`, and `png`) 3. The variable that keeps track of which thing the loop is currently processing (in our case, `filetype`) 4. The body of the loop that does the processing (in our case, `echo $filetype`) In this case, the body uses `$filetype` to get the value of the variable instead of just `filetype`, just like it does with any other shell variable. It can also be noticed where the semicolons go: the first comes between the list and `do`, and the second between the body and `done`. This kind of loop can also be done for files. While individual file names can be used, it is usually better to use wildcards: ```bash for filename in seasonal/*.csv; do echo $filename; done ``` It will print: ```bash seasonal/autumn.csv seasonal/spring.csv seasonal/summer.csv seasonal/winter.csv ``` --- --- But what if I want to run multiple commands within a single loop? Well, this example loop will print the second line of each data file: ```bash for file in seasonal/*.csv; do head -n 2 $file | tail -n 1; done ``` > Note > > Spaces in file names tend to cause issues when workng in the shell. For this reason, filenames with spaces in them must be quoted with either `'` or `"` --- --- But what what if I wish to do many things inside a single loop? Well, a loop can contain any number of commands. The shell just needs to be told where one command ends and the next begins. Semicolons are used for this: ```bash for f in seasonal/*.csv; do echo $f; head -n 2 $f | tail -n 1; done ``` --- [[Upskilling|← Back to Upskilling]]