Skip to main content

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
0 answers
17 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 ...
Andy Ashwood's user avatar
0 votes
3 answers
63 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) '{...
Whitehot's user avatar
  • 145
0 votes
2 answers
57 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 ...
futurewave's user avatar
-1 votes
1 answer
45 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. ...
Piyush Nikhade's user avatar
1 vote
2 answers
164 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 ...
user5211's user avatar
4 votes
2 answers
443 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 # ...
Nickotine's user avatar
  • 489
0 votes
1 answer
163 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 --...
jayvardhan's user avatar
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 ...
Michele Ancis's user avatar
0 votes
4 answers
130 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 ...
teraspora's user avatar
  • 111
-1 votes
1 answer
83 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@...
FriendFX's user avatar
  • 357
0 votes
1 answer
210 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
136 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
3 votes
2 answers
7k 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
  • 519
0 votes
2 answers
742 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
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 ...
geckels1's user avatar
  • 163
0 votes
1 answer
210 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
89 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 -...
Bog's user avatar
  • 1,034
1 vote
0 answers
81 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
47 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
34 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 ...
Julius Hamilton's user avatar
0 votes
0 answers
266 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
4k 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
  • 161
0 votes
3 answers
42 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
127 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
166 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
745 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
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) &...
Jesse Taube's user avatar
-1 votes
1 answer
197 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
75 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
1k 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
  • 323
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: ...
ylluminate's user avatar
0 votes
2 answers
198 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
721 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
62 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
125 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
490 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
210 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
239 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
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 ...
Manuel Jordan's user avatar
0 votes
2 answers
918 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
229 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
413 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
994 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
131 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
197 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
171 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
  • 239
5 votes
2 answers
15k views

How do I make Bash not drop NUL bytes on input from command substitution?

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
  • 189
3 votes
1 answer
497 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
  • 133

1
2 3 4 5
8