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
1
vote
1answer
10 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 ...
0
votes
0answers
16 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 ...
1
vote
0answers
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 "...
3
votes
1answer
66 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 \ ...
22
votes
2answers
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 ...
1
vote
2answers
96 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 ...
1
vote
1answer
61 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 => ...
0
votes
0answers
53 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....
1
vote
2answers
107 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 ...
0
votes
1answer
46 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 ...
2
votes
5answers
166 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? ...
4
votes
4answers
491 views

Command substitution in quotes cannot be used to call the variable

Say: variable="Something that it holds" then echo "$variable" will output: Something that it holds But say I also do: var2="variable"; echo "\$$(echo $var2)" ...
1
vote
1answer
44 views

How to join results of two commands in IBM AIX ksh

I would like to perform script below but without creation of intermediate files (lsfs.out, df.out) on IBM AIX with ksh lsfs_out=`lsfs | sed -n '1d;p' | sort -b -k 3` df_out=`df -k | sed -n '1d;p' | ...
1
vote
0answers
15 views

Sed works standalone but not in command substitution [duplicate]

When I use the following command in Bash, it works fine: sed -n '/##/{p; :loop n; /##/q; p; b loop}' FILENAME.txt It outputs something like this: ## * Some line Command substitution However, when I ...
4
votes
1answer
53 views

Can I determine if the current function has been invoked via command substitution?

foo() { if [[ something ]]; then echo "The foo function was called directly." else echo "The foo function was invoked via command substitution." fi } Call foo ...
2
votes
1answer
55 views

Get exit status of command substitution [duplicate]

If I assign the output of a command substitution to a local variable, how do I get the exit status of the command? This is the behaviour of ZSH 5.8: false; echo $? # output is 1 as expected foo=$(...
0
votes
1answer
74 views

How output one command with tee to standard ouput and also to pipe it to another command?

Maybe this is duplicate, I have not found any answers though, but how to use pipe to output one command to multiple outpus (e.g. send it to pipe, stdout, or command substitution)? I have this line: $ ...
1
vote
0answers
72 views

how to embed command substitution in sed?

from this answer: Using a command inside a sed substitution there is used this to make command substitution: echo aaabbdd | sed -e 's/a*/echo `echo -n & | wc -m`/;e' When I shell use double ...
5
votes
3answers
193 views

Command substitution and double quotes: why results are different?

Here is an example where backticks and $() behave differently: $ echo "$(echo \"test\")" "test" $ echo "`echo \"test\"`" test My understanding was this is because "backslashes (\) inside backticks ...
1
vote
1answer
54 views

Command Substitution Within a Command Substitution

I'm new to shell scripting and wanted to insure I haven't made any errors in creating this script for making a borg backup to a flashdrive that I plug into my computer. Does the below script look ...
0
votes
1answer
129 views

Substitute or merge output of two commands in one, using one line command

How can I merge the output of two commands in one, using one command? command1 output: ID NAME1 COLUMN2 xxx-1 aaa bbb xxx-2 ccc ddd xxx-3 eee fff xxx-4 nnn ...
3
votes
1answer
621 views

Why does a Bash 'while' read loop with a command substitution herestring not read the entire input?

Look at this very basic Bash script: #!/bin/bash while read l do echo $l echo "next one" done <<< $(ps aux) I have several processes on my computer, and the ps aux command works fine in ...
1
vote
1answer
42 views

zsh parameter expansion replacement pattern parses string differently if input is a variable instead of inlined

A zsh parameter expansion replacement pattern parses string differently if input is a variable instead of inlined. I assume that it has something to do with character escaping, but am not sure how to ...
0
votes
1answer
28 views

Passing output of rsync command into notification message (on macOS)

Probably, this is a general unix question and therefore not restricted to macOS. I would like to output the output of my rsync command as a notification message. But I don't succeed in placing the ...
3
votes
1answer
59 views

command substitution does not respect err_exit sometimes

I have a weird situation where inserting any command that succeeds in the line above makes command substitution ($(I mean this)) not work as expected — it does not exit, even though the command fails ...
0
votes
1answer
380 views

bash script variable from curl

I think I am missing something simple here, but here goes. Thsi works from the command line with no problem, it give me the required output. curl -X GET \ -H "X-Auth-Email: REDACTED" \ -H "...
1
vote
1answer
106 views

Getting different value for ps command when assigning to a variable

I get a different output when I assign this to a variable. Why is this? #!/bin/bash P=$(ps -ef | grep ${APP} | grep -v grep | wc -l) echo ps -ef | grep ${APP} | grep -v grep | wc -l echo $P Output:...
1
vote
1answer
54 views

Bash Syntax for Using GREP to Extract Text from File and Store in an Array

I have recently upgraded from bash 4.2.46 to bash version 4.2.50 I had a script with the line: mapfile -t my_array < <(grep ${ID} -w /etc/passwd | cut -d ":" -f 1,4,5) where ID is an ...
2
votes
2answers
171 views

Block all default commands while a bash script are running?

I want to make a custom commands directory, that only works when the shell script are running and when the user stops the script all will fall back to the default without altering the default commands ...
2
votes
1answer
71 views

any way to make this command chain shorter or better

I am using this command chain to filter out bot/crawler traffic and ban the ip addresses. Is there any way i can make this command chain more efficient? sudo awk -F' - |\\"' '{print $1, $7}' access....
0
votes
1answer
126 views

Why does `tr` not work with find and with command substitution? [duplicate]

I want to transform/"translate" all files in a folder from lower to uppercase. I could use a for loop to do that: for i in ./* ; do mv $i $(echo $i | tr "a-z" "A-Z") ; done for i in `find . -maxdepth ...
0
votes
2answers
99 views

Why less won't be displayed during the execution of a command substitution?

I have an interactive script that returns a path for cd to use in the parent process. Like this: $ cd $(myscript) During the execution of the script, I want to be able to display some information in ...
0
votes
2answers
71 views

cd on the last folder tcsh

trying to cd on the last directory in bash if im trying this : cd $(ls -l | tail -l) it will result in : Illegal variable name. I'm not sure how I should do this
2
votes
2answers
122 views

bash: Match and replace user input with another array

First of all, sorry for the heading. I'm not sure how to place my ambition in a few words. However, from a coding/scripting standpoint it should be pretty basic. Though, I somehow fail to achieve it :/...
0
votes
2answers
66 views

How to add 1st / 2nd argument to bash script being sourced from stdin

I have this: bash <( curl_url='https://raw.githubusercontent.com/oresoftware/run-tsc-if/master/run.sh' curl -H 'Cache-Control: no-cache' --silent "$curl_url" ) the bash script at that url, ...
3
votes
1answer
828 views

Combining xargs, sed and command substitution [duplicate]

I would expect under bash for the following: ls *.py | xargs -I{} echo $(echo {} | sed 's/\.py/_2\.py/g') to list all .py files contained in the directory but with _2 annexed after the file name ...
0
votes
3answers
127 views

Automatically hold directory name from inside a tar, by a variable

I download a tarball containing only one directory; this directory has a very long name. I desire to change that directory's name once I extract it from the tarball but I prefer another way than ...
0
votes
3answers
63 views

How do I join the results of a command substitution?

I want to create a list of mp3 files like a 1.mp3|a 2.mp3. I have tried echo ${(j:|:)$(echo *.mp3)} and some variations of it, but they don’t work: # too much splitting 01|The|Magic|Finger|01.mp3|...
2
votes
1answer
68 views

How to split an expression by space in bash?

I am getting the valid result I want in zsh but not bash, what am I missing? I am trying to split the string into words. Bash bash-3.2$ echo ${$(echo "Hello World")[@]} bash: ${$(echo "Hello World")[...
4
votes
4answers
2k views

How to prevent command substitution on the command line?

I find that when writing text as input to another program, any command substitutions in double quotes within the intended text are interpreted and expanded by the shell The links in the answer here ...
1
vote
1answer
809 views

Makefile commands with external args and internal variables

In a Makefile target I have a list of files that I want to split into x parts and work only with one part y, then pass those file names as argument to a test runner. I have no control over the ...
0
votes
1answer
55 views

Repeat commands with changes

I have to repeat this command a lot of times a day, is there any way to execute it multiple times (aprox. 25 times) cd ../folderName && ../../tools/clone.py What I need to do is execute the ...
4
votes
1answer
694 views

Processes in a session in an interactive shell vs in a script

If I run this command in a script it doesn't produce output except header from the ps: # Taken from Advanced Bash Usage on youtube: echo "$(echo "$(echo "$(echo "$(ps wwf -s $$)")")")" This is ...
1
vote
1answer
108 views

Why do I have “command not found” in my script?

I am writing a Nagios plugin and I have a "command not found" when I run it. What is wrong? Why 127.0.0.1 (or localhost as well) is not recognized? When I run just the snmp request - I have a simple ...
1
vote
1answer
130 views

how to execute compound commands in shell script?

Here's my script: #!/bin/sh result=$((diff <(sort 1.txt) <(sort 2.txt))) if [[ $result != "" ]] then echo ERROR else echo PASS fi I got an error when execute this script: chk.sh: ...
1
vote
2answers
1k views

Bash: Escaping double quotes in $() command substitution [duplicate]

I am having trouble wrapping my head around how command substitution works when part of the command's parameters come from a variable. To illustrate, I'll just present a series of commands: I'll ...
0
votes
0answers
42 views

What is difference between `` and $() for command-substitution in bash scripting? [duplicate]

I'm new to Linux and StackExchange. My English is pretty bad, and I'm sorry for mistakes. What is the differences between `` and $()? Does it matter what to use and when? I'll be grateful for ...
1
vote
2answers
84 views

Bash Perl multiline match into variable

I am trying to extract 2 strings from a multiline text. postmsg="\033[0;37m Email Settings" This line finds some match at the beginning of the string and puts it in a variable inCol. inCol=$(perl -...
4
votes
2answers
623 views

NUL delimited variable

GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu) Idea is to set a variable to a NUL delimited data set. Here $samples This, however, result in: warning: command substitution: ignored ...
3
votes
1answer
2k views

Is `echo $TEST` expanding an asterisk in the variable a bug? [duplicate]

Is this a Bash bug? $ mkdir test && cd test && echo "a" > "some.file" test$ echo '*' * test$ TEST=$(echo '*') test$ echo $TEST some.file Why is the second output the resolution of ...

1
2 3 4 5
7