Questions tagged [command-substitution]

Command substitution is the replacement of a command with the result returned after it is evaluated.

234 questions
1
vote
1 answer

bash: Run a command once and save stdout and stderr in their own variables

I need to ensure a command is outputting the correct text. I currently have: command_stdout="$(mycommand --flag 2>/dev/null)" command_stderr="$(mycommand --flag 2>&1 1>/dev/null)" Instead of having to run the same command twice, is there any way I…
leetbacoon
  • 1,111
  • 2
  • 9
  • 32
1
vote
4 answers

Execute command that results from execution of a script whose name is in a variable

When posting this question originally, I totally misworded it, obtaining another, reasonable but different question, which was correctly answered here. The following is the correct version of the question I originally wanted to ask. In one of my…
Enlico
  • 23,259
  • 6
  • 48
  • 102
1
vote
3 answers

Run command in script and check return value

I am trying to run the diff command on two folders, check the return value and then output a message. I have this: #!/bin/bash result='diff dir1 dir2' if result == 0 then echo "OK" else echo "ERROR" fi But I am getting result: command…
user997112
  • 29,025
  • 43
  • 182
  • 361
1
vote
1 answer

Run perl script in bash array

I need to run a perl script in a bash array that will return certain values based on the string you provide. The perl script itself takes a user and a string and it will return the value based on the string you give it. For example. This is how the…
Michael
  • 37
  • 1
  • 7
1
vote
0 answers

Why is '$(< file)' equivalent to '$(cat file)' in bash?

According to bash manual: The command substitution $(cat file) can be replaced by the equivalent but faster $(< file). but I fail to see why. Let's try to take a closer look at what $(< file) means and what it does: by definition, a $(command)…
Peter
  • 313
  • 1
  • 3
  • 10
1
vote
2 answers

How to remove a known last part from commands output string in one line?

To rephrase - I want to use Bash command substitution and string substitution in the same line. My actual commands are longer, but the ridiculous use of echo here is just a "substitution" for shortness and acts the same - with same errors ;) I know…
uldics
  • 117
  • 1
  • 11
1
vote
1 answer

Why isn't a semicolon in command substitution output treated identical to one in the original code?

In my understanding of command substitution this should work, but it doesn't, can you explain me why and how to do something like this. Why does this work: cd ..; echo 123 # output is "123", after changing directories ...when this…
Salmjak
  • 57
  • 5
1
vote
1 answer

Unable to echo last executed within .sh script

Using following on cmd=$(uname -a) printf "["!:0"]" gives me [cmd1=Linux localhost.localdomain 2.6.32-300.10.1.el5uek #1 SMP Wed Feb 22 17:37:40 EST 2012 x86_64 x86_64 x86_64 GNU/Linux] but when is done in test.sh #!/bin/bash output=$(uname…
lazereyes
  • 107
  • 6
1
vote
1 answer

How do I include a variable in a ksh command subtitution?

I'm trying to find a number of lines that match a regex pattern in grep received as a variable. When I do the grep with the pattern directly in the command substitution, it works. When I use a variable for the pattern, it…
Pat N.
  • 35
  • 5
1
vote
2 answers

Parent trap visible but not run by subshell

Tested for Bash 5.0.2 According to the GNU Bash Reference Manual, Bash performs the expansion [of a command substitution] by executing [the] command in a subshell environment According to The Open Group Base Specifications Issue 6: when a…
Marcus Rossel
  • 3,196
  • 1
  • 26
  • 41
1
vote
1 answer

Replace a date string in sed with the ouput of a custom function with arguments

I have a bash script which echos out an html file like this ./foo.sh > out.html In the html there are timestamps in the following format 2019-02-24T17:02:19Z. I wrote a function to convert the time stamp to the time delta between the timestamp and…
user3022779
  • 25
  • 1
  • 3
1
vote
1 answer

Use substituted string as a command in shell script

Given the variables listed below: echo ${userupper}_PYTHON # YTU_PYTHON echo $YTU_PYTHON # /home/ytu/anaconda3/bin/python echo $path # foo.py Now I'd like to execute /home/ytu/anaconda3/bin/python foo.py with userupper and path. I tried…
ytu
  • 1,822
  • 3
  • 19
  • 42
1
vote
2 answers

csh: set: No match

I define a function, an array and a variable: set fnctn = "F(x)=Vx1*(1+cos(1*x-pi))" set Vx = ( 1 1 1 1 ) set Vx1 = $Vx[1] The following commands do what I want: echo "$fnctn" | sed "s/Vx1/$Vx1/" set fnctn2 = `echo "$fnctn" | sed…
fyd
  • 11
  • 1
  • 3
1
vote
4 answers

IFS and command substitution

I am writing a shell script to read input csv files and run a java program accordingly. #!/usr/bin/ksh CSV_FILE=${1} myScript="/usr/bin/java -version" while read row do $myScript IFS=$"|" for column in $row do $myScript …
Mayavi
  • 35
  • 1
  • 4
1
vote
1 answer

Bash script - perform grep on filenames containing particular numbers

I have a bunch of files such as: jobReports/NARX_20191212_1.out jobReports/NARX_20191212_10.out jobReports/NARX_20197695_2.out jobReports/NARX_20197695_3.out jobReports/NARX_20261798_1.out where the first numeral sequence denotes a jobID, and the…
Asy
  • 313
  • 1
  • 3
  • 11