> ## Content Index
> Fetch the complete content index at: https://mfitzp.ghost.io/llms.txt
> Use this file to discover other available public pages before exploring further.

# Bash Command Substitution
- URL: https://mfitzp.ghost.io/bash-command-substitution/
- Published: 2012-01-12T00:00:00.000Z
- Updated: 2023-06-25T18:53:43.000Z
- Author: Martin Fitzpatrick
- Tags: Tutorials, #Import 2024-03-14 08:20, #Import 2026-08-24 14:26

Bash command substitution performs a given command replacing the marker with the resulting standard output. It is particularly useful when you want to store the output of a command in a variable or as an alternative method of chaining multiple commands together.

Bash command substitution is achieved by wrapping your target code in braces with a preceding $, or backticks \`. For example:

```bash
$ date +%d-%b-%Y
21-Jul-2012

```

You can put the output of that command into a variable using command substitution as follows:

```bash
$ today =$(date +%d-%b-%Y)
$ echo today
21-Jul-2012

```

Alternatively, with backtick style:

```bash
$ today =`date +%d-%b-%Y`
$ echo today
21-Jul-2012

```

You can also perform command substitution inside an echo command:

```
echo -e "List of logged on users and what they are doing:\n $(w)"
```

You can also feed the results of command substitutions into a for loop as follows:

```bash
for f in $(ls /etc/*.conf)
do
   echo "$f"
done

```

💡

**This example is a little contrived as you can achieve the same result with `for f in /etc/.conf`*