Command substitution is the replacement of a command with the result returned after it is evaluated.
Questions tagged [command-substitution]
234 questions
0
votes
0 answers
How can I override a bash variable at the time of executing it in console?
I have a bash script that will print yesterdays date. It looks like this:
#!/bin/bash
YESTERDAY_DATE=$(date '+%Y-%m-%d' -d '1 day ago')
echo $YESTERDAY_DATE
and I can run it like: ./script.sh, output as follows:
2021-02-23
Now, I want to be…

Saffik
- 911
- 4
- 19
- 45
0
votes
1 answer
indirect bash command execution not working as expected with coproc
I'm quite new to linux shell scripting and have a question:
1.) why does the command
test1="leafpad" && coproc exec "$test1"
work in bash (commandline, GNU bash 4.4.12 on a debian derivate linux), but the command
test1="coproc" && exec "$test1"…

MI605
- 3
- 2
0
votes
0 answers
find command written in bash script outputs command not found
#!/bin/bash
18 if [ "$arg" == "jpg" ] then
19 "$(find ./"$folder" -type f -name "*.jpg" -exec mv {} "$arg" \;)"
20 fi
This block of code above works and the find and mv command executes and I have the output in the jpg folder but my script…

catBuddy
- 367
- 1
- 3
- 15
0
votes
1 answer
fold: invalid number of columns when running script with arguments
I got a strange behavior when running a script with arguments that contains a command substitution. I would like to understand why is this behavior happening. The script is:
#!/bin/bash
# MAIL=$1
# USER=$2
PASSWORD=$(cat /dev/urandom | tr -dc…

Pulsar
- 3
- 3
0
votes
0 answers
command substitution not working in alias?
I wanted to make an alias for launching a vim session with all the c/header/makefiles, etc loaded into the buffer.
shopt -s extglob
alias vimc="files=$(ls -A *.?(c|h|mk|[1-9]) .gitconfig [mM]akefile 2>/dev/null); [[ -z $files ]] || vim $files"
When…

First User
- 704
- 5
- 12
0
votes
3 answers
bash logic inside command substitution
I have a little banner that I display at login
fignow() { figlet -w $(tput cols) $(date +"%b %d, Week %V"); figlet -w $(tput cols) -f univers $(date +"%H:%M"); }
This works fine if the additional fonts are installed to get univers, but that's not…

YorSubs
- 3,194
- 7
- 37
- 60
0
votes
0 answers
From all the files that their name is composed of 4 letters, which contain the string “user” in their content?
I have to answer this question as an exercise.
Sample input: No sample, just trying to select and filter files using Unix shell according to some conditions
Sample output: a list of files that their name is composed of 4 letters and which contain…

Ruben Cohen
- 1
- 1
0
votes
0 answers
bash get back interpreted escape string
Consider the following bash script
#!/usr/bin/env bash
tstring="Hi\tThere"
istring=$(echo -e $tstring)
echo $istring # Prints: Hi There
rstring=$(printf '%q' "$istring") # This is not doing what I intended
echo $rstring # Prints: $'Hi\tThere'
I…

Vikash Balasubramanian
- 2,921
- 3
- 33
- 74
0
votes
1 answer
command to create file name with date as postfix using shell script
I need to create the dump file name with the current date as postfix to the file name in shell script.
I tried below command in my script but that did not work.
Can any one suggest me the way :
... dump_'$(date '+%Y-%m-%d')'.txt
BACKGROUND:
I feel…

jailaxmi k
- 89
- 3
- 11
0
votes
1 answer
variable error in bash when doing calculation
I assigned output of piping into a variable, but when I try to use the variable to do math, it won't allow me:
%%bash
cd /data/ref/
grep -v ">" EN | wc -c > ref
cat ref
cd /example/
grep -v ">" SR | wc -l > sample
cat sample
echo $((x= cat sample,…

user432797
- 593
- 4
- 13
0
votes
1 answer
How to use the output of one command as the bash completion for another
I'm creating a new command and I need a completion function so that when I press tab a couple of times it gives me some alternatives to choose from. The list of alternatives should be the branches available in a git repository.
I've used complete -W…

Abdallah
- 402
- 4
- 14
0
votes
1 answer
Issues passing variables inside of command substitution in bash
I want to run sort and pass LC_COLLATE=C to it. The output is then going to be stored in a variable.
I wrote this:
var="$(LC_COLLATE=C sort 'aaa
Abc')"
But, oddly enough, I receive sort: No such file or directory. I most definately have sort on my…

leetbacoon
- 1,111
- 2
- 9
- 32
0
votes
2 answers
Using bash variable in input redirection with sort command
I have a bash script written as follows in Jenkins Execute shell block
id="233"
I want to use this id variable inside following command
bash -c 'comm -12 <(sort file1_${id}.txt) <(sort file2_${id}.txt)'
But it throws error
sort: cannot read:…

hruday
- 67
- 1
- 9
0
votes
0 answers
Run command substitution inside function when function is called
I have a bash script which have some helper function inside a shared, common file lib.sh. However, some of these functions look like this:
function check_prs {
local labels
labels=$(hub pr show -h "${BRANCH:-}" -F '%L')
if [[ $PRLABELS…

Christian P.
- 4,784
- 7
- 53
- 70
0
votes
3 answers
Executing the output as filename
In one of my Bash scripts, there's a point where I have a variable SCRIPT which contains the /path/to/an/exe, and what the script ultimately needs to do, is executing that executable. Therefore the last line of the script is
$($SCRIPT)
so that…

Enlico
- 23,259
- 6
- 48
- 102