Trenches have often been dug for defense. In the pre-firearm eras, they were mainly a type of hindrance for an attacker of a fortified location, such as the moat around a castle (this is technically called a ditch). An early example of this can be seen in the Battle of the Trench. With the advent of accurate firearms, trenches were used to shelter troops.
Trench warfare and tactics evolved further in World War I and the Crimean War, until systems of extensive main trenches, backup trenches (in case the first lines were overrun) and communication trenches often stretched dozens of kilometres along a front without interruption, and some kilometres further back from the front line.