Command substitution is the replacement of a command with the result returned after it is evaluated.
Questions tagged [command-substitution]
234 questions
111
votes
3 answers
How do I assign the output of a command into an array?
I need to assign the results from a grep to an array... for example
grep -n "search term" file.txt | sed 's/:.*//'
This resulted in a bunch of lines with line numbers in which the search term was found.
1
3
12
19
What's the easiest way to assign…

ceiling cat
- 5,501
- 9
- 38
- 51
111
votes
3 answers
Backticks vs braces in Bash
When I went to answer this question, I was going to use the ${} notation, as I've seen so many times on here that it's preferable to backticks.
However, when I tried
joulesFinal=${echo $joules2 \* $cpu | bc}
I got the message
-bash: ${echo…

rojomoke
- 3,765
- 2
- 21
- 30
98
votes
3 answers
How to avoid bash command substitution to remove the newline character?
To speed up some bash script execution, I would like to keep the result of a command in a variable using command substitution, but the command substitution replaces the 0x0A newline character by a space. For example:
a=`df -H`
or
a=$( df -H )
When…

Laurent
- 991
- 1
- 7
- 4
64
votes
3 answers
Fish equivalent of bash $(command) notation
I am currently trying out the fish shell instead of using bash. One type of notation I'm having trouble learning the fish-equivalent notation for is $(command), similar to how it is described in this SOF post. How do I write this using fish? Keep in…

ecbrodie
- 11,246
- 21
- 71
- 120
26
votes
1 answer
Exit code of command substitution in bash local variable assignment
How can I check the exit code of a command substitution in bash if the assignment is to a local variable in a function?
Please see the following examples. The second one is where I want to check the exit code.
Does someone have a good work-around or…

Vampire
- 35,631
- 4
- 76
- 102
26
votes
2 answers
When does command substitution spawn more subshells than the same commands in isolation?
Yesterday it was suggested to me that using command substitution in bash causes an unnecessary subshell to be spawned. The advice was specific to this use case:
# Extra subshell spawned
foo=$(command; echo $?)
# No extra subshell
command
foo=$?
As…

Caleb
- 5,084
- 1
- 46
- 65
19
votes
3 answers
PS1 command substitution fails when containing newlines on msys bash
This command succeeds
$ PS1='$(date +%s) $ '
1391380852 $
However if I add a newline it fails
$ PS1='$(date +%s)\n$ '
bash: command substitution: line 1: syntax error near unexpected token `)'
bash: command substitution: line 1: `date +%s)'
If I…

Zombo
- 1
- 62
- 391
- 407
17
votes
4 answers
Command substitution vs process substitution
I am trying to understand the differences between these two similar commands.
aa=$(foo | bar | head -1)
read aa < <(foo | bar | head -1)
I know that <() requires #!/bin/bash, but does that make it slower?
Do they create the same amount of…

Zombo
- 1
- 62
- 391
- 407
16
votes
2 answers
How to set environment variable within GDB using shell command?
I want to run gdb with only a single variable MyVar in its environment.
However, the variable's contents are rather complex, containing non-printable ASCII, and so is best set using e.g. MyVar=$(python -c 'print("\xff...")').
I'm left with two…

Shuzheng
- 11,288
- 20
- 88
- 186
16
votes
3 answers
Command substitution with string substitution
Is it possible to do something along the lines of:
echo ${$(ls)/foo/bar}
I'm pretty sure i saw somewhere working example of something like that but this results in "bad substitution" error.
I know that there are other methods to do that but such a…

Łukasz
- 453
- 1
- 5
- 13
16
votes
3 answers
xargs: command substitution $(...) with pipe doesn't work
I'm trying to write short script, and the following command:
echo "aaa111 bbb111" | xargs -I {} echo {} | sed 's/111/222/g'
returns aaa222 bbb222, which is what I expect.
I expected the next command:
echo "aaa111 bbb111" | xargs -I {} echo $(echo…

Dmitry Frank
- 10,417
- 10
- 64
- 114
13
votes
1 answer
Escape $() syntax in `docker exec`
I'm trying to run a command having a $() as an argument (no sure what that's called) that should be evaluated in a Docker container. For example:
docker exec mycontainer echo $(whoami)
When this is executed, whoami is run first, on the host…

Pieter Jongsma
- 3,365
- 3
- 27
- 30
12
votes
1 answer
Bash command substitution ( $(...) ) forcing process to foreground
Summary: I have a bash script that runs a process in background, and is supposed to work as a normal command and inside a command substitution block such as $(...). The script itself spawns a process that forks to background. It can be reduced to…

dequis
- 2,100
- 19
- 25
11
votes
2 answers
The mechanics of this shell syntax: ${1:-$(
I recently came across this really neat syntax, in the context of creating bash functions which can accept an argument or a stream from STDIN (i.e. can be piped to). On the surface, I understand what's happening here, but I'd like a bit more…

Joe Healey
- 1,232
- 3
- 15
- 34
9
votes
2 answers
How do I use command substition in Dockerfile
In my Dockerfile I need to use command substition to add some environment variables. I want to set
ENV PYTHONPATH /usr/local/$(python3 -c 'from distutils import sysconfig; print(sysconfig.get_python_lib())')
but it doesn't work. The result is…

Thomas Sablik
- 16,127
- 7
- 34
- 62