Command substitution is the replacement of a command with the result returned after it is evaluated.
Questions tagged [command-substitution]
234 questions
0
votes
2 answers
storing the output of ls command in a shell variable
I am using below command to find a most recent file with name "candump"
ls *.log | grep "candump" | tail -n 1
The output is "candump-2018-04-19_131908.log"
I want to store the output filename to a variable in my shell script. I am using the below…

Verma
- 13
- 1
- 1
- 3
0
votes
2 answers
Why does the 'git branch' command have extra output when used via command substitution?
Say I have a git repo containing these items at the project root, which is also my working directory.
dir1 dir2 script.sh
And I have git branches titled
master 077-fix_issue_a 078-fix_issue_b
I find that if I run the git branch command, I get the…

DeepDeadpool
- 1,441
- 12
- 36
0
votes
2 answers
How do I prevent substitution before processing in TCL?
I'm calling a TCL procedure like this-
::f::abstract -procname my_proc -args "-arg1 $a -arg2 $b"
Now here $a itself is something like this-
"ping -c 1 10.1.1.$ip"
When I try to run this, $a is expanded, and it becomes-
-args "-arg1 ping -c 1…

sbhatla
- 1,040
- 2
- 22
- 34
0
votes
1 answer
Command-substitution breaks quoted arguments with spaces
As show below, command-substitution changes the interpretation of quoted command-line arguments. What is going on under the hood, and is there a workaround?
$ cat dumpargs.sh
#! /usr/bin/env bash
for i in "$@"
{
echo "$i"
}
$ cat…

Brent Bradburn
- 51,587
- 17
- 154
- 173
0
votes
2 answers
run command inside of ${ curly braces
I want to alias cd so that it takes me to the root of my current git project, and if that can't be found it takes me to my normal home dir.
I am trying to set HOME to either the git root or, if that can't be found, my normal home variable.
alias…

theonlygusti
- 11,032
- 11
- 64
- 119
0
votes
1 answer
Why does input redirection work differently when in command substitution?
Consider the following case:
$ echo "abc" > file
$ var=$(< file)
$ echo "$var"
abc
Inside the command substitution, we use a redirect and a file, and the content of the file is correctly captured by the variable.
However, all the following examples…

user000001
- 32,226
- 12
- 81
- 108
0
votes
1 answer
Twice Bash command substitution
I have directories a1..a5, b1..b5 and c1..c5. Inside each directory I have two files a1, b1 and c1.
do mkdir /tmp/{a,b}$d; touch /tmp/{a,b,c}$d/{a,b,c}1; done;
I want to get all the files starting with 'a' or 'b' inside the directories starting…

Jose Raul Barreras
- 849
- 1
- 13
- 19
0
votes
2 answers
command substitution in shell script with shell variables within the substitution
Basically the file I'm getting has the first three columns pasted into followed by a column of blanks lines because it looks like nothing is getting appended into column4
I feel like I probably shouldn't be using the variables I created in the…

Sam
- 1,765
- 11
- 82
- 176
0
votes
4 answers
Bad Substitution when assigning stat command output to a variable
I have a script which uses find and chgrp/chmod to recursively set certain permissions and group on a directory which is specified in $1
To extract the group of this target directory, I use
mygrp = ${stat -c %G $mydir}
But executed under bash, this…

AlexC
- 3
- 2
0
votes
2 answers
How to include stdout/stderr redirection with command subsitution when command passed as a variable?
I have the following script to detect when my network comes back on after restarting my router:
#!/bin/bash
pingCommand="ping 192.168.1.1 -c 3 &>/dev/null"
while [[ ! $($pingCommand) ]]; do
sleep 3s;
done
However, when run in the terminal, it…

crimsonspectre
- 103
- 3
0
votes
0 answers
Command Substitution not working
I am trying to store output of command to variable in below bash script but the output is displayed on screen instead of storing in the variable
#!/bin/bash
check=$(ip link show dev mylink)
echo "$check"

MrDev
- 11
- 2
0
votes
0 answers
GNU find and more complex -exec option parameter values
This works for me:
$> find . -name "*.log" -exec basename '{}' \;
20160114.log
20160115.log
20160116.log
20160117.log
20160118.log
Is the {}, \ and ';' mandatory when using -exec as any other syntax simply doesn't work?
The following, more complex…

msciwoj
- 772
- 7
- 23
0
votes
1 answer
linux how to use dynamic variables
I have made a shell script that does some calculations.
The user inputs 2 numbers:
the first number being the month (if desired date is february 2010 for example he puts in 2)
the second number being the year (if desired date is february 2010 for…

Joey
- 154
- 5
- 18
0
votes
1 answer
Streaming command substitution output to terminal w/o squelching color
Is there a way to show the output of a command substitution, in color, while it is executing, while still storing it to a variable? This would mainly be for testing/debugging purposes, and the output would not be shown normally.
If I do…

trysis
- 8,086
- 17
- 51
- 80
0
votes
2 answers
This statement runs interactively but not from 'at' scheduled
if [ $(/sbin/iptables -w -L INPUT -n|grep --line-buffered -m 1 -c -w 66.128.56.213) == "0" ]; then /sbin/iptables -w -I INPUT "$(/sbin/iptables -w -L INPUT -n --line-numbers|stdbuf -o0 grep -m 1 -w DROP|stdbuf -o0 awk '{print $1}')" -i eth0 -s…

kenneth558
- 89
- 8