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.

Filter by
Sorted by
Tagged with
0 votes
1 answer
70 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:...
Scoobie's user avatar
  • 101
2 votes
2 answers
77 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 ...
dmgeurts's user avatar
2 votes
2 answers
3k 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)" ...
Lee's user avatar
  • 491
0 votes
2 answers
262 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 --...
Guerric P's user avatar
  • 103
0 votes
0 answers
33 views

process substitution vs command substitution

I am trying to use vcf-merge command. The command works like this: vcf-merge f1.vcf.gz f2.vcf.gz f3.vcf.gz I have bunch of vcf.gz files so I did something like this: vcf-merge <(ls *.vcf.gz | tr &...
user3138373's user avatar
  • 2,441
0 votes
0 answers
33 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 ...
geckels1's user avatar
  • 123
0 votes
1 answer
108 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 "...
Leighton 's user avatar
0 votes
1 answer
60 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 -...
Pixelbog's user avatar
  • 540
1 vote
0 answers
50 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 # ...
anonymous's user avatar
0 votes
2 answers
36 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 " ...
Eduardo's user avatar
0 votes
0 answers
33 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 ...
hmltn's user avatar
  • 143
0 votes
0 answers
54 views

Bash command substitution in function with redirect

I am trying to pass a command that contains I/O redirection to a function. The function executes the command using command substitution and redirects stderr to stdout. The command should return an AWS ...
SteveTR's user avatar
0 votes
0 answers
122 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 ...
Anuraag Tummanapally's user avatar
0 votes
1 answer
218 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 ...
vrms's user avatar
  • 139
0 votes
3 answers
34 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 ...
Nir's user avatar
  • 1
0 votes
1 answer
100 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://...
Joe Burden's user avatar
0 votes
1 answer
81 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 ...
God of Money's user avatar
3 votes
2 answers
446 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 ...
Alapati Srikanth Chowdary's user avatar
1 vote
1 answer
27 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) &...
Jesse Taube's user avatar
-1 votes
1 answer
148 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....
Sebastian Nielsen's user avatar
-1 votes
2 answers
68 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 ...
Lawrence's user avatar
  • 329
2 votes
2 answers
668 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 ...
t7e's user avatar
  • 293
0 votes
2 answers
739 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: ...
ylluminate's user avatar
0 votes
2 answers
163 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 ...
Fajela Tajkiya's user avatar
1 vote
2 answers
399 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 ...
simonmysun's user avatar
0 votes
1 answer
58 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"...
TheGameiswar's user avatar
3 votes
1 answer
109 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 ...
JohnnieL's user avatar
  • 133
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=&...
tres.14159's user avatar
0 votes
1 answer
370 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 ...
Avi Shukron's user avatar
2 votes
1 answer
189 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')"
Nor Vegia's user avatar
1 vote
2 answers
195 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 '...
eracesa's user avatar
  • 13
5 votes
3 answers
4k 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 ...
Manuel Jordan's user avatar
0 votes
2 answers
842 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=$(...
Manuel Jordan's user avatar
0 votes
3 answers
159 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-...
Manuel Jordan's user avatar
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+...
Manuel Jordan's user avatar
0 votes
1 answer
234 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,...
Manuel Jordan's user avatar
0 votes
2 answers
881 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 ...
Nitz's user avatar
  • 143
0 votes
1 answer
105 views

Incorrect order of commands in "Command Substitution" (Bash/Dash)

Why is it that when you run the substitution commands, the order of execution is broken, as in the example below. First the chmod command was executed, then echo 1 and echo 2? echo $(echo 1; echo 2; ...
NewLinux's user avatar
  • 147
0 votes
1 answer
105 views

Wrong arguments being passed when sourcing script using command substitution [duplicate]

I have a script print.sh: #!/bin/bash echo printing provided args: for i in "$@"; do echo -e "\t${i}" done If I do this at the prompt a=$(. print.sh ); echo "${a}&...
guest's user avatar
  • 77
2 votes
1 answer
96 views

Backticks giving different result than pasting result

GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu) Ubuntu 20.04.3 LTS So, I downloaded like 20 7z files and I wanted to move them somewhere, but NOT the previous 7z files I already had in ...
Erhannis's user avatar
  • 229
3 votes
2 answers
10k views

How do I ignore "command substitution: ignored null byte in input"?

I have this Linux shell command: echo $(python3 -c 'print("Test"+"\0"+"M"*18)') | nc -u [IP] [PORT] My intention is to pipe the output of the print statement to the ...
Joker's user avatar
  • 169
2 votes
1 answer
365 views

Bash: command substitution with background job

I'm trying to fire and forget a process within a function in bash. I'd this function to return a status corresponding to the fact the process has indeed been fired and retrieve output of the function ...
Momh's user avatar
  • 123
-2 votes
1 answer
681 views

How to run a command using another command output as part of its text in terminal?

If command1 is: curl -k -v -u user:password https://example.com/v2/image/manifests/tag -H Accept: application/vnd.docker.distribution.manifest.v2+json 2>&1 | grep Docker-Content-Digest | awk '{...
McLan's user avatar
  • 269
8 votes
1 answer
4k views

Makefile command substitution

My Makefile: all: ...(other rules) clean clean: rm $(find . -type f -executable) When I delete clean rule from the above Makefile everything works as expected. After adding, make (also make ...
Elgin Cahangirov's user avatar
1 vote
1 answer
3k views

"bash: syntax error near unexpected token `('" error with process substitution

In a fresh, clean bash instance I get as expected the following: $ printf "a\td\n" | cat -vE a d$ $ $ file <(printf "a\td\n" | cat -vE) /dev/fd/63: symbolic link to pipe:[...
The Quark's user avatar
  • 260
1 vote
1 answer
2k views

Environment variable expansion inside $(command substitution)

I'm running Bash 5.1.4 on Debian. I'm writing a post-installation script to copy configuration and other files to locations in my home directory. I add the intended destination to each file at the ...
mathbekunkus's user avatar
3 votes
2 answers
209 views

Why does the command echo `echo \\\\\\\z` in bash script print \\z instead of \\\z?

The command echo `echo \\\\\\\z` is from this book , I don’t understand why it prints \\z when it get executed via the bash script. I think it should print \\\z
ll cool's user avatar
  • 31
4 votes
3 answers
2k views

How to have Bash interpret output from command as quoted strings?

I have a program that gets the files selected in the graphical IU (in my case Finder in macOS). The output is something like '/tmp/file number one.txt' '/tmp/file number two.txt' Note the space char ...
Markus's user avatar
  • 41
1 vote
1 answer
58 views

Bash: Difference between paste command with Command Substiution vs. FIFO FDs

I'm just trying to understand why when I use a couple FIFO FDs in a paste command I get a 'Broken Pipe' but when I use Command Substitution everything is all good. A little background: I have a need ...
Wafflemann's user avatar
0 votes
1 answer
340 views

Saving hashed string in a variable

I need to hash a string with SHA-512 algoithm. I have a salt also. I'm trying in the script this: inputHash=$( perl -e 'print crypt("${array[3]}","\${pass[0]}\$${pass[1]}\$") . &...
areverte's user avatar

1
2 3 4 5
8