Questions tagged [command-substitution]
Questions about shell command substitution (such as $(command) or `command`), its mechanism, correct syntax etc. Not to be confused with aliasing.
409 questions
0
votes
2
answers
128
views
Process sed capture group with a bash function before replacement: "sh: 1: <bash function>: not found"
So, I was playing around with this answer, when I found that neither
printf_stdin () {
read input
printf "$input"
}
sed "/lorem ipsum foobar/ {
s/'/'\"'\"'/g
s/\...
2
votes
1
answer
521
views
Using a literal asterisk (*) in a filename within command substitution in bash
how do you supply a literal asterisk * when specifying a filename to a command substitution in bash?
Consider the following directory:
host:/tmp/backtick-test# ls -l
total 0
-rw-r--r-- 1 root wheel ...
0
votes
2
answers
88
views
One-liner piping from find/xargs with paths including spaces
The following question likely does not relate specifically to Vim. I use a Vim example, as this is where I encounter the issue.
Working on Ubuntu, I often open multiple files in Vim using tab pages:
$ ...
1
vote
1
answer
141
views
Multiline command substitution - syntax errors with mysterious `+1`
I'm trying to break a long command substitution on to multiple lines, as discussed in this answer.
In a plain command pipeline, both explicit (\) and implicit line continuation work fine:
$ echo 'blah ...
0
votes
1
answer
74
views
command substitution and Dollar sign before variable
I have a file named fileWithOneCommand.txt with just one command as follows
ps -aux|head -n 5
then I write a testing shell script named 'test5.sh' with content as follow:
file=/home/somepath/...
0
votes
1
answer
49
views
Command substitution used with tput which doesn't print anything
I am doing a code-study of /lib/lsb/init-functions on mint 20 just to familiarise myself with SystemV init system.
There is log_action_end_msg function in it which has this code snippet,
TPUT=/usr/bin/...
-4
votes
1
answer
127
views
Why is command substitution resulting in 'command not found error'?
So doing
$(cat /etc/passwd)
results in "No such File or Directory"
and
"$(whereis cat)"
results in "command not found"
Why is command substitution not working in the ...
0
votes
1
answer
135
views
bash - check free available space against requered
in bash i will run a script that take a look in the folder folder123/ to know how much space is requered for the files they are in there.
but this requered value will i multiplie with 1,5 and than ...
1
vote
1
answer
102
views
How can I iterate over the white space separated words returned by a command substition?
I have the following simple shell command:
for x in $(echo one two three); do echo $x; done
When executed, it simply prints
one two three
on one line, i. e. the result of the command substitution is ...
0
votes
1
answer
83
views
Nested command substitution doesn't work, but hardcoded results work
I am trying to pass filenames retrieved from everything voidtools via their es.exe command line (it works similarly to mlocate) to an "image previewer" like gwenview or nsxiv (in wsl ...
0
votes
0
answers
50
views
Command Substitution doesn't work with spaces [duplicate]
I'm trying to pass a list of image files to open gwenview. The only way that seems to work is to list them on command line. However, some file paths have spaces, so they have to be additionally quoted....
0
votes
3
answers
133
views
Pass output of command line to awk variable
I am trying to normalise a data file using the number of lines in a previous version of the data file. After reading these questions, I thought this could work:
awk -v num=$(wc -l my_first_file.bed) '{...
0
votes
2
answers
104
views
Bash scripting: When to use variable, when function?
basic, innocent question:
In bash scripting, why ever using a function, if one can set a variable containing command substitution with the essence of the function - a certain command or set of ...
-1
votes
1
answer
162
views
zgrep command not working correctly on command substitution [duplicate]
I am preparing a script
that will try to detect two texts on the same line.
This pattern is present in my application log files,
which then further used to identify which file contains the pattern.
...
1
vote
2
answers
331
views
How to make bash abort a command on an inline execution error
Command substitution, or Inline Execution as I call it, $() in Bash allows for text returns; before the execution of a main command. How do I abort the main command, if a $() returns an error code
...
4
votes
2
answers
485
views
SSH for loop: parameter passed to function captured in variable is not expanded
I have several servers which have several files with deploy date info I need to parse, and get a local copy of the files which are 2 months old or older.
#!/bin/bash
# split on new line not space
# ...
0
votes
1
answer
611
views
unexpected EOF while looking for matching `'' when using backticks as variable
The below code is working:
[ec2-user@ip restore]$ echo $snap_name
manual-test-2024-01-11-11-26-19
[ec2-user@ip restore]$ aws rds describe-db-cluster-snapshots --db-cluster-identifier creditpoc3 --...
0
votes
0
answers
89
views
output of cat when file contains asterisks and quotes [duplicate]
I have a command stored in a text file command which looks like this:
command_name par1 par2 path/with/backslashes -option_1 3 -option_2 0.05 --option_3=* -p 'P001=A' -p 'P002=B' ...
The three dots ...
0
votes
4
answers
295
views
How can I use parameter expansion together with command substitution?
I want to create 12 directories named after the months. So I tried using locale to get the month names, piping to tr to separate them with commas, then put braces around and passed to mkdir. But ...
-1
votes
1
answer
168
views
Why doesn't realpath --relative-to work in a command substitution?
user@debian:~/test/B$ find ..
..
../A
../A/x
../A/y
../A/z
../B
user@debian:~/test/B$ find ../A -type f -print0 |xargs -0 -i% realpath --relative-to=../A %
x
y
z
user@debian:~/test/B$ # But
user@...
0
votes
1
answer
330
views
When simply `| cat -`, I get file descriptor errors
Here's the working diff command:
$ diff -u <(echo 'foo:bar:baz' | tr : "\n") <(echo 'foo:baz' | tr : "\n")
--- /dev/fd/11 2023-08-30 13:11:50
+++ /dev/fd/13 2023-08-30 13:...
2
votes
2
answers
255
views
Parse command with expanded variables
Trying to test something and not quite nailing it either way I've tried, so not sure if I'm being silly or not.
Essentially I need to parse a string to ipa-getcert's post-save argument which should ...
3
votes
2
answers
13k
views
Bash variable substitution into yq command
Given a YAML file, example.yaml:
node:
sub_node:
get_this:
I'd like to get a variable containing get_this using Mike Farah's yq and the string sub_node
yaml="$(cat example.yaml)"
...
0
votes
2
answers
1k
views
Command substitution not working with environment variable
I have this command which I want to variabilize. Particularly I want the latest argument presence to depend on an environment variable.
ansible-playbook --inventory inventories/ssg-dev deploy.yml --...
0
votes
0
answers
43
views
What does !^ (exclamation mark caret) do in bash? [duplicate]
Does anyone know what the command !^ does in the bash shell? I know ! relates to history expansion and ^ relates to command substitution (when it's not being used in a regular expression when it ...
0
votes
1
answer
261
views
Loop to Monitor Line Count Growth Using wc -l
I've written a short bash script to monitor the line count growth (wc -l) in a file which is receiving results from a loop.
Thus:
printf "Name of file to monitor\n"
read file
printf "...
0
votes
1
answer
128
views
How to pipe output of a command to another command (which does not take a file as input?) [duplicate]
What I want
Simplified command: echo "helloworld" | echo $1
I know, this would be possible with cat, but what if a command like echo does not take a file as input?
My actual command: ls -...
1
vote
0
answers
115
views
How should alias in command substitution behave?
I tested a few scripts with different shells, and the results differ.
# test 1
foo(){ echo $(al);}
alias al='echo 123'
foo
# test 2
alias al='echo 123'
foo(){ echo $(al);}
alias al='echo abc'
foo
# ...
0
votes
2
answers
76
views
How to atribute multiple values to an array from a command substitution?
Consider the following shell script function:
#!/bin/bash
declare -a dir
function() {
local -a directories=( "A/B/C D" "E/F G H" ) #Initialize local array.
printf "%q " ...
0
votes
0
answers
56
views
Distribute subcommands over head command to avoid writing same words twice?
I would like to try to write this command:
while read -r repo; do gh repo delete $repo; done <<< $(gh repo list --no-archived)
which basically cleans up my GitHub and deletes everything that ...
0
votes
0
answers
429
views
recursive history search for tcsh shell
In bash shell, i generally use CTRL+R to fuzzy search a command that i executed recently.
However the same doesn't seem to work for tcsh or csh shells.
I want to understand if/how to do the same ...
0
votes
1
answer
12k
views
referencing a ${variable} value inside a sed substitution
I am trying to replace a string within a couple of similar sql scripts.
in my toolbox I have a for loop as well as grep and sed inside a bash script.
#!/usr/bin/env bash
for efa_instance in ...
1
vote
3
answers
104
views
Iterate over a multiline variable and extract a particular line and use it as a command
The variable I wish to extract will look something like this
ttyACM0
ttyACM1
I would like to iterate over this variable and issue the command:
echo disconnect > /dev/ttyACM0
and then
echo ...
0
votes
1
answer
153
views
Syntax Question for Bash using pipes
I am trying to set a variable to a value returned by a program (lynx). I cannot get past which parenthesis or bracket I use to accomplish my goal.
I have this:
DEBFILE=${(lynx -listonly -dump https://...
0
votes
1
answer
215
views
How to take the number including dot from an output of one command and use it in another one?
How to take the number including dot from an output of one command and use it in another one?
For example, this command
chia wallet show -w standard_wallet
gives this result
Wallet height: xxxxxx
...
4
votes
2
answers
1k
views
Parameter substitution inside indirect expansion
I want to achieve the parameter substitution inside indirect expansion, but I cannot achieve it with a single command.
I get a "bad substitution" error. Let me know if there's a clever way ...
1
vote
1
answer
36
views
Can bash_history save evaluated commands output?
Is there a way for bash or other shells' history to save the output of evaluated commands when it's a parameter?
$ echo $(pwd) && history
/root
0 I want something like:
1 echo $(pwd) &...
-1
votes
1
answer
218
views
Call command with one argument being the result of cat'ing a file
How do I call a command with on argument being the result of cat'ing a file?
npx aws-api-gateway-cli-test \
--username $username \
--password $password \
--method $method \
--body cat user....
-1
votes
2
answers
133
views
How to use the string returned by $() to be used as more than one argument in another command?
My use case:
I need to remove all dev packages listed in composer.json file. Suppose I have two packages: projectx/package-nice and projecty/package-good. To remove them I need to run:
$ composer ...
2
votes
2
answers
2k
views
Echoing/printing a bash `ls` command substitution without quotes [duplicate]
I have the following example:
$ a="$(ls)"
$ echo $a
backups cache crash lib local lock log mail opt run snap spool tmp
$
$ echo "$a"
backups
cache
crash
lib
local
lock
log
mail
...
1
vote
2
answers
2k
views
fish shell: why does `file `which command`` work in zsh and bash, but not fish?
Before switching to fish shell, I frequently used various commands in zsh with which some_command. An example might be:
$ file `which zsh`
/opt/local/bin/zsh: Mach-O 64-bit executable arm64
/bin/zsh: ...
0
votes
2
answers
228
views
Is it possible to generate multiple complete commands with command substitution?
There are many commands that accept multiple parameters, like touch, ls and rm. So that I can use the construct {1..3} to generate multiple parameters for the command. For example:
[root@192 ~]# touch ...
1
vote
2
answers
1k
views
How to execute a string of bash command with command substitution
Suppose I have a bash variable like this:
tmp1='$(echo foo)'
or
tmp2='`echo foo`'
How to achieve foo as result?
I know that removing the command substitution from the string should work, but is ...
0
votes
1
answer
85
views
Why does $(RANDOM) work differently from $(ls -la)?
for command substitution in bash, we do this
hello=$(ls -la)
echo "$hello"
but when i do the same for below command,it fails,it says RANDOM not found
hell=$(RANDOM)
echo "$hell"...
3
votes
1
answer
139
views
Don't run command when command substitution result is blank
I have the following code to run pylint on all files in a Git repository with file extension .py, fed by command substitution.
pylint $(git ls-files '*.py')
The script runs just fine except when ...
0
votes
0
answers
22
views
Handling whitespaces in the parameters into a command substitution [duplicate]
I have troubles with whitespaces and command substitution. I made a tiny script to show the problem:
#! /bin/bash
a="toilet -t --font='Dot Matrix' --filter metal --filter border ABC"
#a=&...
0
votes
1
answer
656
views
Command substitution with xargs inputs [duplicate]
I'm trying to write a simple command that will create symlinks for all files in a specific directory (Yes, exactly like lndir but I cannot use it so I try to mimic it).
I tried the following using ...
2
votes
1
answer
228
views
What's the differences? (command substitutions) [duplicate]
printf "%s" `echo ../.. | sed 's/[.]/\\&/g'`
printf "%s" $(echo ../.. | sed 's/[.]/\\&/g')
printf "%s" "$(echo ../.. | sed 's/[.]/\\&/g')"
1
vote
2
answers
292
views
Can someone tell me the difference between these three command substitutions?
I am asking why these three commands give three different answers:
$ printf "%s\n" `echo ../.. | sed 's/[.]/\\&/g'`
&&/&&
$ printf "%s\n" $(echo ../.. | sed '...
9
votes
3
answers
7k
views
passwordless check to see if a user can use 'sudo'
I want to check if a user is able to use sudo - but without the need to write the password - for bash shell scripts purposes. I did some research and I found:
Sudo - is there a command to check if I ...