Command substitution is the replacement of a command with the result returned after it is evaluated.
Questions tagged [command-substitution]
234 questions
2
votes
1 answer
Why basename with a suffix doesn't work in a subshell when used with find?
The following command doesn't do the subtitution, why?
find ./ -name "*.dng" -exec echo `basename \{\} .dng` \;
but this command work:
find ./ -name "*.dng" -exec basename \{\} .dng \;
What I'm actually trying to do is to find all the dng in my…

Fractale
- 1,503
- 3
- 19
- 34
2
votes
2 answers
Consistent syntax for obtaining output of a command efficiently in bash?
Bash has the command substitution syntax $(f), which allows to capture
the STDOUT of a command f. If the command is an executable, this is fine
– the creation of a new process is necessary anyway. But if the command is
a shell-function, using this…

kdb
- 4,098
- 26
- 49
2
votes
1 answer
Process substitution broken by a pipe
I did a silly thing today:
read x <( ps -fu $LOGNAME | grep ' /usr/bin/ps$' )
It hung, never returned. I had to break it.
Some of you are quite rightly laughing at me right now. :)
It only took me a minute to see why this doesn't work, but I wanted…

Paul Hodges
- 13,382
- 1
- 17
- 36
2
votes
1 answer
Why is this command substitution waiting for the background job to finish?
I'm trying to get the PID of a background job with command substitution. The background job is started with setsid. The problem is that the parent process is struck at the command substitution.
Here is an example script:
#!/bin/bash
if [ "$1" =…

phkb
- 143
- 1
- 5
2
votes
1 answer
command substitution in docker CMD
Using:
CMD ["$(pipenv --venv)/bin/python3", "main.py", "/root/uploads"]
Causes an error on docker run:
Error response from daemon: invalid header field value "oci runtime error: container_linux.go:247: starting container process caused \"exec:…

category
- 2,113
- 2
- 22
- 46
2
votes
2 answers
How to quote command substitution so that it behaves like "$@"?
$* is equivalent to $1 $2 $3... - split on all spaces.
"$*" is equivalent to "$1 $2 $3..." - no splitting here.
"$@" is equivalent to "$1" "$2" "$3"... - split on arguments (every argument is quoted individually).
How to quote $(command) so that it…

Peter C
- 73
- 5
2
votes
1 answer
bash doesn't run commands in sequential order
I have a bash script that contains the following:
MY_COMMAND="MY_PWD=`pwd`; export MY_PWD; MY_PWD_BASENAME=`basename $MY_PWD`; echo $MY_PWD_BASENAME"; export MY_COMMAND
When I source the script from the terminal, I get the following…

Morad
- 2,761
- 21
- 29
2
votes
1 answer
Blank variable during bash command substitution
I am doing command substitution and saving the result to a variable. However, the results of the command contain double quotes and this is causing the variable to be empty.
When running test="$(java -version)" I get the following result:
openjdk…

shavi
- 33
- 3
2
votes
1 answer
bash: pgrep in a commad substition
I want to build a small script (called check_process.sh) that checks if a certain process $PROC_NAME is running. If it does, it returns its PID or -1 otherwise.
My idea is to use pgrep -f in a command substitution.
If I run this code…

eddie
- 1,252
- 3
- 15
- 20
2
votes
1 answer
Command substitution and $PATH variable
Background
This [ article ] says :
The command substitution expands to the output of commands. These
commands are executed in a subshell ..
But the bash manual says nothing about a subshell in its command substitution section.
My test below
$…

sjsam
- 21,411
- 5
- 55
- 102
2
votes
1 answer
bash command substitution on external script function receives incorrect exit status
This example was tested on Mac El Capitan with bash
main_script.sh:
NOTE: func_a and func_b are identical except for the line that the local variable output is declared.
func_a () {
local output
output="$(./external.sh some_function)"
…

Arctelix
- 4,478
- 3
- 27
- 38
2
votes
1 answer
How to pass a ruby script's output as arguments to a shell command using command substitution?
I have the following in a_script.rb:
if ARGV.empty?
puts "string_without_spaces 'string with spaces'"
else
p ARGV
end
When I run:
ruby a_script.rb `ruby a_script.rb`
I get the following output:
["string_without_spaces", "'string", "with",…

Judas Iscariot
- 83
- 7
2
votes
2 answers
double backslashes of sed single-quoted command inside command substitutions get translated to a single backslash
printf '%s' 'abc' | sed 's/./\\&/g' #1, \a\b\c
printf '%s' "`printf '%s' 'abc' | sed 's/./\\&/g'`" #2, &&&
The expression inside the second backticks returns \a\b\c, and we have printf '%s' "\a\b\c", so it should print…

pdg
- 103
- 8
2
votes
2 answers
How to use case/esac in process substitution?
I've the following line which works fine:
while getopts :t t; do case $t in t) echo $t; break; esac; done
however when I'm trying to use it as command substitution, the bash fails with error.
Code:
#!/usr/bin/env bash
echo $BASH_VERSION
[ "$(while…

kenorb
- 155,785
- 88
- 678
- 743
2
votes
1 answer
Command Substitution in Bash for tty
Command:
echo "a" | tee `tty`
Output:
a
Command:
echo "a" | tee /dev/pts/0
Output:
a
a
File connected with current terminal is /dev/pts/0.
Shouldn't both commands produce same output? What am I missing here?

user148865
- 326
- 1
- 5
- 19