Questions about shell command substitution (such as $(command) or `command`), its mechanism, correct syntax etc. Not to be confused with aliasing.

learn more… | top users | synonyms

0
votes
0answers
18 views

command substitution within c style for loop in Zsh

I have a function printing a number myFunc() { echo 11 } Now I want to use the number in expression in c style for-loop: for (( i=1; i<=$(myFunc); i++ )); do echo "$i" done Zsh gave ...
2
votes
2answers
36 views

Measure execution time of a command including expansion of command substitution

How would I go about measuring the running time of commands that are expanded by the shell? For example: ➜ time sleep $(sleep 3; echo 1) sleep $(sleep 3; echo 1) 0.00s user 0.00s system 0% cpu 1....
0
votes
1answer
28 views

how to get the standard output from command in variable

when we want to know what is the standard output of the ssh then we do ssh $linux_server "cat /proc/uptime" [[ $? -eq 0 ]] && ssh is ok but how to know the ssh standard output in case of ...
10
votes
1answer
286 views

How does ksh93 avoid forks in command substitution

Given cmd='fun(){ echo "$@"; }; fun $(fun $(fun hi))' shells tend to need to make 2 forks to make it happen strace-f(){ strace -f "$@" 2>&1; }; for sh in dash bash zsh ksh; do ...
2
votes
3answers
468 views

Pass output of “whereis” command to “cd” to change directory in one step

It should be simple no-doubt, but I can't find a way to pass output of whereis command to cd command in same line so I don't have to do cd in the second step. I have tried passing like below: cd $(...
-4
votes
1answer
38 views

Back Tick version of this command [closed]

TAROUTPUT=$(tar cvf /tmp/incremental_backup . tar $(find /etc -type f -mtime -1)) Please suggest how can i use backtick for above command
5
votes
2answers
356 views

nested double quotes in highly-voted one-liner

A StackOverflow answer with > 3.5K votes features this one-liner for assigning to DIR the directory of the current bash script: DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" I'm ...
1
vote
2answers
56 views

Bad substitution trying to rename extension [duplicate]

I'm trying to convert all wav and mp3 files to ogg files recursively with find and FFmpeg. I have made the following bash script to do so: #!/usr/bin/env bash # Converts samples to ogg find samples ...
1
vote
1answer
44 views

Output broken when appending output of sql query in ksh

I'm very new to scripting. I would like to add the output of the sql function given below to another variable for example: month_end () { mdate=$($SQLPLUS_HOME/sqlplus.exe -S $DBC << END set ...
0
votes
2answers
26 views

Preventing command substitution from evaluating when appending to a file

How can I stop the eval statement from evaluating when it's being appended to a file as part of a cat << __EOF__ command? TL;DR I manage services on a large number of Linux servers. ...
4
votes
1answer
38 views

bash/cygwin broken after installing rtool?

Usually I can solve problems on my own (and Google). But recently I am baffled by this strange hiccup. Background: I use a chain of scripts for file-processing (in Windows, under Cygwin. Scripts ...
0
votes
1answer
36 views

tcsh preserve newlines in command substitution `…`

So, I wrote a simple script to convert sh-style export key=value statements to csh-style setenv key value for docker-machine env. #!/bin/sh docker-machine env | sed -e 's/export/setenv/' -e 's/=/ /' -...
3
votes
1answer
95 views

Multiple variable assignment from different parts of one command pipeline

Say I have 2 variables that I am assigning as follows: psaux=`ps aux | grep someProcessName` pscount=`ps aux | grep someProcessName | wc -l` I would like to combine the assignments in one line, so ...
1
vote
1answer
15 views

Command substitution with pkg-config in Fish

Fish's command substitution is supposed to be the equivalent of Bash's $() yet this simple example fails: g++ -std=c++14 -Wall -Wextra -pedantic -g (pkg-config --cflags sdl2) \ test.cpp (pkg-...
0
votes
1answer
40 views

Linux starting bash issue : command substitution issue

I try to start a new shell in my environment, but bash command has an issue: $ bash bash: command substitution: line 1: syntax error near unexpected token `then' 'ash: command substitution: line 1: `...
1
vote
1answer
31 views

Command substitution. Spawned process name is identical to the parent one

This script $ cat csub.sh #!/bin/bash while true; do sleep 5 AVAR=$(date; sleep 2) done when started, makes a process appear in the ps output: ps -eLf|egrep '[c]sub.sh|PID' UID PID ...
0
votes
1answer
55 views

command substitution - cat file inserts quotes

In bash, given these files: $ cat noquotes.txt s/a/b/g $ cat quotes.txt "s/a/b/g" Why does $ echo "aaa" | sed -e $(cat noquotes.txt) bbb succeed, but $ echo "aaa" | sed -e $(cat quotes.txt) sed:...
1
vote
4answers
56 views

Bash file - find value from command output and keep as variable

I am trying to make a script that will conntect to a ups and get the battery value and store it in a variable. I have managed to connect to the ups and get infomation from it but I am strugling to ...
1
vote
1answer
32 views

Substitute with `…` multiline output of find command [duplicate]

I need to substitute output of find command to another command to process every found file, e.g.: mdls `find ~/target_dir/ -iname '*some*' -depth 1` (mdls is a command in OS X which get metadata ...
0
votes
2answers
53 views

Is a script running inside command substitution?

In a POSIX-compatible shell, is it possible to determine if a script is running inside command substitution? my-outer $(my-inner) $(my-other-inner) For the above example, my-inner and my-other-...
3
votes
2answers
37 views

How is the return status of a variable assignment determined?

I have seen constructs in scripts such as this: if somevar="$(somecommand 2>/dev/null)"; then ... fi Is this documented somewhere? How is the return status of a variable determined and how does ...
0
votes
1answer
51 views

capture exit code and output of a command

There are similar questions, but mine is a little bit different. Or so I think. What I'd like to do is: 1.sh: #!/usr/bin/env bash set -eu r=0 a=$(./2.sh || r=$?) echo "$a" echo "$r" 2.sh: #!/usr/...
1
vote
1answer
70 views

How to escape a range of wildcards in unix shell script?

I need to execute the following Oracle SQL in unix crontab. The query is as follows: select count(*) from tbaadm.htd where cust_id is not null and pstd_flg = 'Y' and del_flg = 'N' and tran_date = (...
2
votes
3answers
73 views

Processing a single file as both input and output throughout pipes [duplicate]

Good evening, I would like to filter a file's content with some piped commands and then write the result back to the same file. I know, I can't do that the way I wrote it. Hold on … This is the ...
1
vote
1answer
46 views

Bash: Default variable with command substitution

Is there any way to accomplish the following in one stroke using Bash? my_var=${$(grep -E '^setting_1' /settings.conf):-default_setting1} As opposed to: my_var=$(grep -E '^setting_1' /settings.conf)...
1
vote
2answers
48 views

How do I create a rule for custom flag replacement with C shell?

I want to have the following behavior with grep: grep --java <rest of the command> Where --java would either dynamically convert to --include=*.java or internally be equivalent to it. Is ...
0
votes
1answer
45 views

How do I execute command to avoid interference from the shell [duplicate]

Sometimes a same command is provided by shell-builtin as well as other file/package. Example: $ type -a printf kill printf is a shell builtin printf is /usr/bin/printf kill is a shell builtin kill is ...
2
votes
1answer
43 views

Error exit script from within command substitution

If I have a function: myfunction() { [ -d somedirectory ] && [ "some other condition" ] || { echo "error" >&2 ; exit 1; } global_var=somevalue } And I call it from within another ...
1
vote
2answers
110 views

Backtick command encapsulation

test@debian:~$ echo `echo \`echo "uh!"\`` uh! How does the Bash do that? It seems that it first executes the expression in the non-escaped backticks, which gives back (the double-quotes "" are ...
1
vote
1answer
65 views

Bash : ambiguous redirect

I am trying to collect dump of java process with jstack and I have tried below command. But its saying ambiguous redirect as I cant redirect to a command. jstack -l `pgrep java` > /tmp/TD_`...
3
votes
2answers
28 views

vi — replace chars from part of line

I've got a file with all Belgian cities. Some of these cities have boroughs. Lier;stad;Antwerpen;49,70;33930;Lier;Koningshooikt Knokke-Heist;gem.;WestVlaanderen;56,44;33825;Knokke;Westkapelle;Heist;...
0
votes
0answers
47 views

How to write into local file and set its content to a variable within a ssh command substitution

I would like to write on local file (or local variable if better) and set its content to a variable within a ssh command substitution. Here is my code : REMOTE_OUTPUT=$(ssh remote@server /bin/bash &...
1
vote
2answers
92 views

How to preserve the carriage return character (\n) when capture output of a command in a variable

As a simple example, I have a bunch of source code files. I want to store the "head" command output to a variable for all these files. I tried: output=$(head $file) but what happened is that this ...
-1
votes
2answers
188 views

Nested echo command in backticks

I want to know why an extra echo will print in my shell. I'm using bash version 4.2.46(1). echo `echo `echo $SHELL`` A interesting issue is if I replace '``' with $() it doesn't print extra echo: ...
0
votes
1answer
63 views

Thunar custom action: Extraction to subdirectories

I am attempting to create a Custom Action in Thunar (File Manager) that will extract a gzip archive into a subdirectory of the same name (e.g. abc.tar.gz to abc/). I created this command, which works, ...
1
vote
1answer
31 views

Command substitution

I would like to create a script on SUSE Enterprise Linux, it first asks for a string (representing a date) and then search for all files in a certain directory which contains the string in its ...
2
votes
2answers
91 views

For input redirection, are there functional differences between bash/zsh <<<“$(<file)” and the traditional <file?

I'm asking only about the usage which would have the similar effect as traditional input redirection from a file. <<<"$(<file)" as far as I can tell is equivalent to <file It ...
1
vote
1answer
48 views

Having trouble with img2pdf and spaces in filenames

I am trying to convert a bunch of jpeg files to a pdf file. When I type the filenames with the quotes all is fine: $ img2pdf "./Page 001.jpg" "./Page 002.jpg" -o book.pdf When I go for the ...
6
votes
3answers
390 views

How to prevent bash command substitution output from being escaped?

I am trying to use command substitution in a bash script to output redirection symbols based on a variable like so: IS_VERBOSE=false curl $BLAH $( $IS_VERBOSE && echo '-i' || echo '> /dev/...
1
vote
2answers
56 views

xargs repl_str doesn't expand second placeholder

I would like to rename some files and exclude others into a directory. I tried find . -mindepth 1 -maxdepth 1 ! -name 000-default.conf ! -name default-ssl.conf -print0 | xargs -0 -I {} sudo mv -- {} $...
14
votes
6answers
2k views

Why there is such a difference in execution time of echo and cat?

Answering this question caused me to ask another question: I thought the following scripts do the same thing and the second one should be much faster, because the first one uses cat that needs to open ...
2
votes
1answer
123 views

In bash, grep for a line beginning with \, and assign the result using backticks

In bash, I'm trying to grep a file for a line beginning with a \, and return the result using backticks. For example: echo \\Hello > myFile out=`cat myFile | grep '^\\Hello'` echo $out returns ...
3
votes
2answers
44 views

Command substitution as target for I/O redirection

I was trying to have the output of a command as a target of redirection like this: echo .envrc >> $(git config --global core.excludesfile) But I get this error instead, where ~/....
2
votes
4answers
41 views

slicing different words from output

Suppose, the result of a command in linux shell is as follows: X and Y are friends Is there any way to slice each of the words (X, and, Y, are, friends) or the first n words from the result, so ...
2
votes
2answers
54 views

why my first pipe operation didn't remove newline character?

My nextermCensus.lis contains newline character and I am only interested in the first line, I'd like to remove newline character. but it doesn't work. so I have to use second method to remove it. $(...
0
votes
2answers
92 views

Returning local values from Bash variables?

I studied this article called Returning Values from Bash Functions. Data Lorem. \begin{document} hello \end{document} Case #1 which does not work Code #!/bin/bash function getStart { ...
2
votes
3answers
4k views

echo and command substitution

I wrote a very basic script of command substitution which is below: #!/bin/bash files=$(ls *.fastq); echo $files The directory contains bunch of .fastq file and I just want to use echo command to ...
1
vote
1answer
73 views

bash: cut command produces nothing (just a blank line)

I have a problem with being able to echo a part of a string Here is a simplified version: t=no_1187985:/home/my.name/testing/no I want to echo no_1187985 only so: tt=$(echo $t | cut -d":" -f 1) ...
19
votes
1answer
551 views

Why won't function return until background process ends?

Consider this script: #!/bin/bash function start { leafpad & echo $! } PID=$(start) echo "PID is $PID" The script does not continue past its closing brace until the leafpad process ends, ...
5
votes
1answer
5k views

Bash script error: integer expression expected

I have a problem with a bash script on raspberry pi: x='gpio -g read 22' if [ $x -ge 1 ] then gpio -g write 23 1 fi The error is integer expression expected. Why?