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.
403 questions
-4
votes
1
answer
65
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
41
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
48
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
43
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
47
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
0
answers
19
views
bash variable problem [duplicate]
Apologies if this is nooby question but I am new to bash scripting and I couldn't find anything similar.
I am trying to use ffmpeg to re-encode a load of files and that requires I pull out some ...
0
votes
3
answers
84
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
69
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
88
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
220
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
444
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
496
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
88
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
168
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
101
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
257
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
178
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
9k
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
979
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
40
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
234
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
102
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
94
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
54
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
38
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
318
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
6k
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 ...
0
votes
3
answers
47
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
133
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
194
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
899
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
30
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
206
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
91
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
...
0
votes
2
answers
1k
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
203
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
938
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
70
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
130
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
532
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
220
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
255
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 '...
6
votes
3
answers
6k
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 ...
0
votes
2
answers
965
views
How to use string substitution in one command with command substitution?
When the git branch command is executed it usually shows:
* main
.... # other branches
Having the following through command substitution and string substitution work as expected:
current_branch=$(...
0
votes
3
answers
236
views
How to retrieve the command executed by command substitution for log reports
For Simplicity, consider the following:
x=$(java --version)
echo "x: $x"
It works how expected showing:
x: openjdk version "11.0.13" 2021-10-19
OpenJDK Runtime Environment Temurin-...
0
votes
0
answers
28
views
Behaviour for output of Command Substitution working with String variable with Pipes [duplicate]
For simplicity and how source/input of this post for the coming scenarios, observe the following
java --version
openjdk 11.0.13 2021-10-19
OpenJDK Runtime Environment Temurin-11.0.13+8 (build 11.0.13+...
0
votes
1
answer
468
views
mount cifs: how to retrieve the status of the command execution from the terminal?
The following mount cifs code works fine:
sudo mount -t cifs //192.168.1.77/something /mnt/192.168.1.77/something \
-o username=alpha,rw,uid=alpha,gid=tango,dir_mode=0770,file_mode=0770,...
0
votes
2
answers
1k
views
Bash: Alias not recognized in shell interpolation
~$ bash --version
GNU bash, version 5.1.12(1)-release (x86_64-pc-linux-gnu)
~$ alias bab=python
~$ $(echo bab)
bash: bab: command not found
I'd expect bab to be turned to "python", but it ...