• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Justin Joyce

Practical tips and tutorials about software development.

  • Standing Invitation
  • Featured Posts
  • Latest
  • About

Bash ampersand (&)

Posted May 10, 2023 — Updated Jan 10, 2024

The single ampersand & is used to run commands asynchronously in the background.

From the bash docs:

If a command is terminated by the control operator ‘&’, the shell executes the command asynchronously in a subshell. This is known as executing the command in the background, and these are referred to as asynchronous commands. The shell does not wait for the command to finish, and the return status is 0 (true).

Bash & can be very useful, but it does come with a small catch—you have to remember to kill the task.

Let’s do an example.

Running a background task

At my day job, we occasionally need to connect directly to services in staging (or production in an emergency). It’s a two-step process, but for ease-of-use we do it in a single command, so it requires a background task. Here’s how it works:

  1. Start a proxy in the background with &
  2. Use the proxy to connect directly to a staging/production database

Here’s a pared down version of the shell command:

# start the proxy
cloud_sql_proxy -instances=more:info:here &

# connect to postgres via the proxy
psql -h 127.0.0.1 -p 1337 -U username -d database_name

As written above, this command has a subtle bug; we have to remember to kill the background task. The cloud_sql_proxy was run in the background via &, which means that when we exit the postgres shell via exit or ctrl c, the proxy won’t stop. In this case, the proxy task is pretty light weight, so it doesn’t matter too much, but it can cause issues or just be confusing later on.

There are many ways to kill processes, and you’ll often see & used in combination with the trap command for that purpose.


Helpful Links

  • How to kill a running process – me!
  • The Bash trap command – also me!
  • Lists of commands – Bash docs

Filed Under: Command Line, Shell

Primary Sidebar

Recent Posts

  • Every Built-In Vim Color Scheme (with screenshots)
  • Reverse a string in Python
  • Meeting Cost Calculator
  • Vim find and replace
  • What makes an effective development team

Categories

  • Arrays (5)
  • Command Line (9)
  • Dates (3)
  • Featured (7)
  • Git (7)
  • Golang (5)
  • Javascript (8)
  • Productivity (8)
  • Projects (4)
  • Python (15)
  • Regex (2)
  • Ruby (3)
  • Shell (2)
  • Thoughts (2)
  • Tips (11)
  • Tools (3)
  • Tutorials (1)
  • Vim (4)

Archives

  • July 2024 (1)
  • February 2024 (1)
  • January 2024 (1)
  • December 2023 (1)
  • November 2023 (1)
  • October 2023 (4)
  • September 2023 (1)
  • August 2023 (2)
  • July 2023 (5)
  • June 2023 (3)
  • May 2023 (6)
  • April 2023 (5)
  • March 2023 (5)
  • February 2023 (10)
  • January 2023 (6)
  • December 2022 (7)

Copyright © 2025 · Contact me at justin [at] {this domain}

  • Privacy Policy