2022 Developer Survey is open! Take survey.

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
2 answers
26 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 ...
user avatar
1 vote
2 answers
43 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 ...
user avatar
0 votes
1 answer
43 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"...
user avatar
3 votes
1 answer
31 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 ...
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=&...
user avatar
0 votes
1 answer
36 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 ...
user avatar
2 votes
1 answer
70 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')"
user avatar
2 votes
2 answers
113 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 '...
user avatar
  • 23
4 votes
3 answers
1k 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 ...
user avatar
0 votes
2 answers
148 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=$(...
user avatar
0 votes
3 answers
48 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-...
user avatar
0 votes
0 answers
26 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+...
user avatar
0 votes
1 answer
44 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,...
user avatar
0 votes
2 answers
117 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 ...
user avatar
  • 153
0 votes
1 answer
41 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; ...
user avatar
  • 127
0 votes
1 answer
39 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}&...
user avatar
  • 47
2 votes
1 answer
44 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 ...
user avatar
  • 219
3 votes
2 answers
2k 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 ...
user avatar
  • 169
1 vote
1 answer
95 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 ...
user avatar
  • 113
-2 votes
1 answer
82 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 '{...
user avatar
  • 269
1 vote
1 answer
376 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 ...
user avatar
1 vote
1 answer
101 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:[...
user avatar
  • 234
0 votes
0 answers
13 views

How to get only the first n characters of a console-command like openssl rand -base64 14 [duplicate]

How do I get only the first n characters of a console-command like openssl rand -base64 14 (for password creation) if this was in a variable I could probably use Substring Expansion, but how do I ...
user avatar
  • 303
1 vote
1 answer
455 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 ...
user avatar
3 votes
2 answers
142 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
user avatar
  • 31
4 votes
3 answers
675 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 ...
user avatar
  • 41
0 votes
1 answer
248 views

Bash parameter expansion `substituted` versus `assigned`

I am looking at parameter expansion using ${parameter:-word} versus ${parameter:=word}. Here is some relevant documentation: ${parameter:-word} If parameter is unset or null, the expansion of word is ...
user avatar
  • 317
1 vote
1 answer
34 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 ...
user avatar
0 votes
1 answer
89 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]}\$") . &...
user avatar
3 votes
1 answer
343 views

Script function call: function vs $(function)

Taking how reference the following code for simplicity #!/bin/bash number=7 function doSomething() { number=8 } doSomething echo "$number" It prints 8. But with: #!/bin/bash number=7 ...
user avatar
0 votes
1 answer
83 views

Why Bash doesn’t pass the errexit option to command substitutions?

Shell options set by set builtin are inherited in sub-shells (at least the errexit). This can be proved by: set -o errexit function foo() { echo "foo:$BASHPID" false echo 'after'...
user avatar
  • 1,073
0 votes
3 answers
114 views

Is parameter expansion in test expression possible?

When I try the following expression in bash I get a strange error message: [: -lt: unary operator expected First the function definition some_func () { (( 3 + 5 )) } And the expression [ $(...
user avatar
  • 303
0 votes
1 answer
88 views

Pipe vs Command substitution [duplicate]

Testing with the linux command line and pipe I tried to do echo {1..3} | touch and it doesn't work. I achieved the results with touch $(echo {1..3}). Why I can't pass the list generated by echo to ...
user avatar
0 votes
2 answers
56 views

using command subsitition in a bash for loop

I am probably making an obvious mistake but I could use a strong hint:-) for file in *; do ls $file | var=$(ls $file | grep -Eo '.{18}$' | cut -d '.' -f1 |sed 's/.\{12\}/&./'); echo "var is $...
user avatar
-1 votes
1 answer
47 views

Downloading a file with a trivial name without overriding a possible existing file with the same name

In CentOS with Bash I download a file with a very trivial file name, install.sh. Although extremely unlikely in my scenario, this could still, in theory, overwrite some existing file with the same ...
user avatar
3 votes
2 answers
228 views

Prevent command substitution from running when declaring a variable

So i recently noticed that, when declaring a variable using a command substitution, let's say like so: var=$(echo "this is running" > test; cat test) Then it will run (the file test will ...
user avatar
0 votes
2 answers
156 views

echo hello && $(echo "world") fails with "bash: world: command not found" [duplicate]

the full command is % echo hello && $(echo "world"), where % is the shell prompt. why does this fail? I've noticed that the error is the same as if I had run the command substitution ...
user avatar
0 votes
1 answer
128 views

Why are command substitutions executed first?

I'am working on Bash shell. When I enter commands in the following way cmd1 $(cmd2) $(cmd3) the order of executing the commands is: cmd2 -> cmd3 -> cmd1 When I look at the shell operation order ...
user avatar
  • 1,082
0 votes
1 answer
309 views

Get output of shell function used inside spec file

Hi I need to get the output (generated via echo) from a function inside a RPM spec file. The function is get_server_ip() { some staff echo $nodeip return 0 } It is located in ...
user avatar
1 vote
1 answer
25 views

Run remote command while adding key to agent, to avoid passphrase prompt interfering with the script

How can I run a command at remote Host2 via remote Host1, while adding my key to the ssh-agent, so that the passphrase prompt does not interfere with the latter part of the script? I am basically ...
user avatar
0 votes
0 answers
17 views

cat a file containing "?" in command substitution [duplicate]

To automate access to my scanner, I'm trying to cat the output of scanimage -L into a Bash variable. In fact I was successfully doing this until recently with siL="$(scanimage -L | grep net | awk ...
user avatar
  • 134
1 vote
0 answers
37 views

How do I build a bash command in a script [duplicate]

I'm missing something simple here. I'm trying to build up a command line in a script, then execute it. Here is the only version that actually works: curl -s -F "files[]=@$file1" -F "...
user avatar
4 votes
1 answer
448 views

subshell not propagating error, bash 4.4

I have this in a github workflow - run: bash --version - name: run postgres run: | set -e shopt -s inherit_errexit export ID=$(docker run \ ...
user avatar
24 votes
2 answers
2k views

The old ticks vs parentheses issue: confused

Since being corrected many years ago, I switched from backticks to $() for command expansion. But I still prefer the backticks. It is fewer keystrokes and does not involve the Shift key. I understand ...
user avatar
3 votes
2 answers
1k views

Escaping double quotes inside command substitution

I am attempting to write a simple bash parser. I am following the steps in this wiki. One of the assumptions I make is that I can do a pass over the entire input string and remove all single and ...
user avatar
  • 157
1 vote
1 answer
168 views

Command substitution inside double quotes

I am attempting to write a bash parser. Many resources have referred to this wiki One area I am getting stuck is why the following would work echo "$(echo "hi")" # output => ...
user avatar
  • 157
0 votes
0 answers
91 views

SSH Command on remote server

I'm stuck in a situation, I run this command on node2 netstat -tlpna|grep `ps aux|grep tnslsnr|grep -v grep|awk '{print $2}'`|head -1 and it works fine and gives me this output tcp 0 0 10....
user avatar
  • 3,834
1 vote
2 answers
367 views

Bash option to make command substitution preserve trailing newlines

I'm hoping that bash comes with an option that stops command substitution from stripping trailing newline characters. Is there? If there isn't, then do any shells exist that are bash-like, and for ...
user avatar
  • 3,408
0 votes
1 answer
94 views

How to configure reportbug to use smtp password from gnome-keyring?

When configuring reportbug, I have an option to store the password for the remote smtp service in $HOME/.reportbugrc. How can I make it use an existing password stored by GNOME Web (Epiphany) or ...
user avatar
2 votes
5 answers
736 views

Using output of expression in bash string slicing

I want to extract a substring of a filename from a path of unknown length. I can do these two parts separately, but I wonder if there is a way to combine the two without a temporary variable? ...
user avatar
  • 178

1
2 3 4 5
8