---
title: "Trapping ctrl+c in Bash scripts"
url: https://michalasobczak.pl/posts/trapping-ctrlc-in-bash-scripts.html
author: "Michał Sobczak"
description: "If you happen to have some Bash scripts and would like to abort it in clean way there is way to do it. You need to trap ctrl+c command. In case you have some while loop in your scripts, without it, pressing any abort commands is only half way effective."
published: 2022-08-13T07:22:29.000Z
updated: 2022-09-06T10:45:14.000Z
language: en
categories: ["Technology"]
tags: ["Bash"]
license: CC BY-NC-ND 4.0 (https://creativecommons.org/licenses/by-nc-nd/4.0/)
---
# Trapping ctrl+c in Bash scripts

If you happen to have some Bash scripts and would like to abort it in clean way there is way to do it. You need to trap ctrl+c command. In case you have some while loop in your scripts, without it, pressing any abort commands is only half way effective.

```bash
#!/bin/bash

trap ctrl_c INT

function ctrl_c() {
  echo "** Trapped CTRL-C"
  exit
}

while
do
  echo -n "."
done
```

Now, with ctrl+c trapped, you can cleanly abort your script without things still running.
