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
2answers
971 views

Bash - Problems creating array from a command output which has quoted text with spaces

For an script I'm making I need to convert the output of a command to an array. For simplifying I have made an example using echo: arr=( $(echo '"test example" "test2 example"') ) What I want is ...
0
votes
3answers
535 views

issue with back quotes inside while loop when running a command over ssh

This question is a bit complicated. I will try to explain in detail. I have two machines, one is local and other say 192.168.1.2. I have set the following variables in my local machine CHECK="/home/...
2
votes
2answers
449 views

stumped by issuing commands through ssh

I am writing a bash shell script that runs on a jump server. This has one parameter, which represents a string to be searched for. This parameter is worked into a `-surrounded command (command ...
-1
votes
2answers
118 views

How to avoid a variable containing a list of directories being treated as string [closed]

#!/bin/bash Dir='/home/TEST' Project="${Dir}/Project.txt" The text file Project.txt contains the file type & its path: SOURCE|FILE|TARGET_PATH TEST|BDL|/LOAN/NDL/XML TEST|BDL|/LOAN/DL/XML TEST|...
5
votes
1answer
5k views

Can I get the exit code from a sub shell launched with $(command)? [duplicate]

I am setting a variable like this: myvar=$(command --params) I want to check the exit code ($?) of my command afterwards. Checking $? like this always returns 0 because it successfully set the ...
0
votes
1answer
113 views

Linux - how to get count of line in file [duplicate]

Can you tell me how to get the line count in a script? PID_COUNT = 'cat /david/file1/test.sh | wc -l ' is not working, shows an error "illegal -l".
6
votes
3answers
1k views

Using binary data as a parameter in bash - any way to allow nuls?

So I'd like to pass the first 512 bytes of binaryFile.dd as the second parameter to myProgram but bash strips out all the NUL chars. Is there any way to avoid this in bash or am I on a hiding to ...
3
votes
1answer
225 views

backticks inside brace expansion

I'm trying to list the files of httpd processes, and I tried this: ls -l /proc/{`pidof httpd | tr ' ' ','`}/ ls: cannot access /proc/{{28493,28492,28491,28490,28489,28488,28487,28486,28483}}/: No ...
10
votes
2answers
503 views

Assignments are like commands with an exit status except when there's command substitution?

See the following examples and their outputs in POSIX shells: false;echo $? or false || echo 1: 1 false;foo="bar";echo $? or foo="bar" && echo 0: 0 foo=$(false);echo $? or foo=$(false) || ...
20
votes
1answer
980 views

What does POSIX require for quoted here documents inside command substitution?

In this question someone reports a problem using a here document with a quoted delimiter word inside $(...) command substitution, where a backslash \ at the end of a line inside the document triggers ...
4
votes
1answer
524 views

Access variables set inside command substitutions

I wrote a simple script #!/bin/bash -x selentry=`ls -l / | sort ; ts=${PIPESTATUS[0]}` echo $ts but $ts show nothing. How I can display $ts variable, or how I can get exit status code from ...
21
votes
4answers
11k views

What's the difference between `curl | sh` and `sh -c “$(curl)”`?

One easy install method for Docker (for example) is this: curl -sSL https://get.docker.com/ | sh However, I have also seen some that look like this (using the Docker example): sh -c "$(curl -sSL ...
3
votes
1answer
474 views

zsh: command substitution does not inherit stdin from its parent

Consider the following command: seq 5 | grep $(tail -n1) <(seq 9) When running it in zsh1: tail: error reading 'standard input': Input/output error Now running the same in bash, it outputs: 5 ...
5
votes
3answers
357 views

Why doesn't the conditional work when I use it directly without a variable?

This gives me an error that says too many arguments: if [ $( file -b $i ) == "directory" ] But when I tried this name=$( file -b $i ) if [ name == "directory" ] It seems to work just fine. Can ...
0
votes
1answer
138 views

shell script run by xautolock keeps stall value in command substitution

I'm trying to make a simple script to put my computer to sleep if it runs on battery unattended. I have this simple script: #!/bin/sh if [ `cat /sys/class/power_supply/ADP1/online` -eq 0 ]; then ...
4
votes
3answers
2k views

Capture the output of a shell function without a subshell

I have rbenv (ruby version manager) installed on machine and it works like that: $ rbenv local 2.3.1 Writing to stdout the local version of my ruby. I want to rescue this version and declare it in a ...
4
votes
1answer
6k views

Can I safely ignore: “warning: command substitution: ignored null byte in input”?

Is it possible to safely ignore the aforementioned error message? Or is it possible to remove the null byte? I tried removing it with tr but I still get the same error message. this is my script: #!/...
1
vote
2answers
1k views

move subshell output into an array in bash 3

How do I import the results of a subshell command into an array in bash 3? I have been using mapfile to accomplish this in bash 4 as follows: mapfile -t myarray < <(someScript.sh) However, ...
0
votes
1answer
58 views

How to create variable from piped data

The command I using is: ls -l . | totLines=$(wc -l) echo $totLines . My goal is display the total number of lines, but by assigning the output of wc -l a variable name, then displaying that variable's ...
2
votes
2answers
1k views

inline heredoc instead of file

I have a command (php file) which takes, as an argument, the location of a file. eg. $ php ./ScriptName.php /my/file/location.txt Sometimes I only need one or two words, so I don't really want to ...
5
votes
1answer
5k views

Using a command inside a sed substitution

I am trying to use sed to replace a sequence of the same characters with the amount of characters in that sequence, but somehow something goes wrong. I am trying the following: echo aaabbdd | sed -e ...
7
votes
2answers
564 views

Any way to show each step during the command processing?

set -x just show a final result of the command. Any way to view each step more clear, like pics below.
1
vote
2answers
193 views

Why a value of a variable doesn't change inside backticks?

Here's my test code: a=1 echo $a echo `let ++a` echo $a The output that I see is 1, 1. Why doesn't the third line modify the value of a?
4
votes
2answers
2k views

Saving command output to a variable in bash results in “Unescaped left brace in regex is deprecated”

Errata: Similar questions about this have been asked but after searching this for a few days there appears to be no answer to this specific scenario. Description of the problem: The second line in ...
6
votes
2answers
1k views

command substitution within single quotes for alias

Double quotes are required in bash for command substitution: $ echo "$(date)" Fri Oct 28 19:16:40 EDT 2016 Whereas single quotes do not do command substitution: $ echo '$(date)' $(date) … ...
2
votes
3answers
528 views

Bash: Quotation in command substitution

In short, I want to use directories listed by a command in a find command: find $(produces_dir_names --options...) -find-options... The problem comes with white space in the directory names. I ...
-2
votes
1answer
101 views

How to install the '!!' command?

I was trying to use !! on my new install of debian and I get the following error: $ sudo !! sudo: !!: command not found I do I gain use of !!? Also what can I call !! so that I can actually google ...
1
vote
2answers
121 views

Use output from find command to then remove a specific directory

When i run this find command: find /html/car_images/inventory/ -type f -iname \*.jpg -mtime -4 i get output like this: /html/car_images/inventory/16031/16031_06.jpg /html/car_images/inventory/16117/...
1
vote
1answer
981 views

Job Control: How to save output of background job in a variable

Using Bash in OSX. My script has these 2 lines: nfiles=$(rsync -auvh --stats --delete --progress --log-file="$SourceRoot/""CopyLog1.txt" "$SourceTx" "$Dest1Tx" | tee /dev/stderr | awk '/files ...
4
votes
3answers
748 views

error=$(mkdir test 2>&1) What is the meaning of that expression?

I have the following shell script: error=$(mkdir test 2>&1) I know that the variable 'error' will get the error result of the mkdir command if there is an error, but I can't understand how 2>&...
0
votes
1answer
294 views

What's the difference between substitution and piping to bash

I am running these two commands: echo $(find ./ $OPT1 $OPT2 $OPT3) echo find ./ $OPT1 $OPT2 $OPT3 | bash and the weird thing is, the top command yields no results while the bottom one does. Why is ...
3
votes
3answers
283 views

Why doesn't shell expansion on popd remove a directory from stack?

When I use popd alone it removes a directory from the stack and takes me to that directory. However, if I do cd $(popd) then no directory is removed from the stack. Since the process is simply forked ...
4
votes
3answers
72 views

What special character might echo be expanding to my home folder?

I was running this single line for: for i in `pwgen -yB -N 8 1`; do echo "$i"; done Then the output is: . Descargas Documentos Escritorio Imágenes Música NetBeansProjects Plantillas public_html ...
11
votes
4answers
40k views

Bash multiplication and addition

for k in {0..49}; do a=$(($((2*$k))+1)); echo $a; done Hi, I need a simplified expression for the third line, maybe one that does not use command substitution.
3
votes
2answers
2k views

Command works at the command-line, but won't work in a shell script [duplicate]

I am creating a script and when I try to capture a command return, I have an error of command not found, if I use this command on the terminal: gcloud -q compute snapshots list --format='csv(NAME)' ...
3
votes
2answers
1k views

Default assignment to a null variable with command substitution

I'm trying to use the syntax: A=${B:-C} where A is the variable, B is the value I'm trying to assign, C is the default value if B is null. Now I want to replace B with the command nc -l 443, so if ...
1
vote
1answer
324 views

Can Bash process substitution be used as part of argument expansion?

I'm familiar with process substitution used the canonical way, e.g. diff <(ls /folder1) <(ls /folder2) However, can it be used when the process asks for an argument that is not just the path ...
1
vote
2answers
485 views

Quoting the output of a backtick command [duplicate]

I'm trying to run an expect script that runs a telnet session to automate the sending of an email, I'm trying to pass the contents of a file as a single argument to my script but when the backticks ...
1
vote
1answer
614 views

Assign continuous stream to variable and process the variable

I'm trying to make an analyzer for a custom log that I have. My log file has a duration timestamp (in ms) which I want to filter out from the others and send me the logs that take over a threshold ...
0
votes
1answer
102 views

Writing output of command to variable [closed]

I would like to put the output of a command into a variable. As far as I know, you have to use either of these: variable=$(command) variable=`command` The following should work from my point of view:...
1
vote
0answers
136 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 me ...
3
votes
2answers
554 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
156 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 ...
12
votes
1answer
523 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 printf ...
2
votes
3answers
2k views

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

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 $(whereis node_modules) Or cd "`...
-4
votes
1answer
321 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
20
votes
2answers
5k 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
751 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
140 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 ...
1
vote
2answers
440 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. ...