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
108
votes
6answers
87k views

How can I execute `date` inside of a cron tab job?

I want to create a log file for a cron script that has the current hour in the log file name. This is the command I tried to use: 0 * * * * echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log ...
263
votes
6answers
26k views

What's the difference between $(stuff) and `stuff`?

There are two syntaxes for command substitution: with dollar-parentheses and with backticks. Running top -p $(pidof init) and top -p `pidof init` gives the same output. Are these two ways of doing the ...
24
votes
5answers
5k views

Why does shell Command Substitution gobble up a trailing newline char?

As per the following example, and as in my recent question In bash, where has the trailing newline char gone?, I want to know "why" it happens x="$(echo -ne "a\nb\n")" ; echo -n "$x" | xxd -p # ...
104
votes
6answers
325k views

Pass the output of previous command to next as an argument

I've a command that outputs data to stdout (command1 -p=aaa -v=bbb -i=4). The output line can have the following value: rate (10%) - name: value - 10Kbps I want to grep that output in order to store ...
14
votes
3answers
5k views

Trapping errors in command substitution using “-o errtrace” (ie set -E)

According to this ref manual: -E (also -o errtrace) If set, any trap on ERR is inherited by shell functions, command substitutions, and commands executed in a subshell environment. The ERR ...
4
votes
2answers
341 views

Command substitution declaration syntax in bash - which of these two is better practice? [duplicate]

This is part of a bash find loop, and I wondered which is more correct syntax and why? filename="$(echo "$i" | cut -c5-)"; filename=`echo "$i" | cut -c5-`; Both function for the purpose of getting ...
121
votes
3answers
97k views

Quoting within $(command substitution) in Bash

In my Bash environment I use variables containing spaces, and I use these variables within command substitution. Unfortunately I cannot find the answer on SE. What is the correct way to quote my ...
20
votes
1answer
982 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 ...
35
votes
3answers
60k views

Understanding backtick (`)

I am trying out the command $ b=5; echo `$b`; -bash: 5: command not found but it does not print 5 as it is supposed to. What am I missing here? What does ` (backquote/backtick) mean in commands? ...
25
votes
5answers
66k views

Why do newline characters get lost when using command substitution? [duplicate]

I have a text file named links.txt which looks like this link1 link2 link3 I want to loop through this file line by line and perform an operation on every line. I know I can do this using while loop ...
15
votes
2answers
3k views

Where has the trailing newline char gone from my command substitution?

The following code best describes the situation.  Why is the last line not outputting the trailing newline char?  Each line's output is shown in the comment.  I'm using GNU bash, version 4.1.5 ...
28
votes
6answers
29k views

Command substitution: splitting on newline but not space

I know I can solve this problem several ways, but I'm wondering if there is a way to do it using only bash built-ins, and if not, what is the most efficient way to do it. I have a file with contents ...
15
votes
1answer
2k views

What's the right way to quote $(command $arg)?

It's high time to solve this conundrum that's been bothering me for years... I've been meeting this from time to time and thought this is the way to go: $(comm "$(arg)") And thought my view was ...
9
votes
2answers
1k 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 ...
8
votes
2answers
720 views

Which shells don't support dollar-parenthesis expansion, and demand backticks?

The modern recommendation is to use: $(...) instead of the older: `...` mainly for readability and to avoid the need for escaping when nesting. Which shells do not support the $(...) form, and ...
6
votes
2answers
178 views

Do I need to quote command substitutions when assigning their output to a variable? [duplicate]

I tend to quote command substitutions as shown below even when assigning their output to a variable: var="$(command)" Is that actually needed though? When does it break? The accepted answer here ...
10
votes
2answers
506 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) || ...
5
votes
2answers
7k views

What is command substitution in a shell? [duplicate]

I'm using Ubuntu 16.04 with Bash and I tried to read in Wikipedia, in here and in here, but I failed to understand what is the meaning of "command substitution" in shell-scripting in general, and in ...
11
votes
2answers
12k views

How do I diff the outputs of two commands? [duplicate]

How can I use the diff command to compare 2 commands' outputs? Does something like this exist? diff ($cat /etc/passwd) ($cut -f2/etc/passwd)
10
votes
4answers
4k views

Understanding Bash's Read-a-File Command Substitution

I am trying to understand how exactly Bash treats the following line: $(< "$FILE") According to the Bash man page, this is equivalent to: $(cat "$FILE") and I can follow the line of reasoning ...
7
votes
2answers
2k views

Difference of using () and $() to execute a series of commands

I am currently trying to make a script that creates bytes that will be piped as input to netcat. Here is the idea of the script: (perl -e "print \"$BYTES\x00\"; cat file; perl -e "print \"More ...
11
votes
5answers
39k views

grep files from list

I am trying to run grep against a list of a few hundred files: $ head -n 3 <(cat files.txt) admin.php ajax/accept.php ajax/add_note.php However, even though I am grepping for a string that I know ...
5
votes
2answers
4k views

When should one use $( ) in defining variables

Some shell scripts I have come across use the following syntax when defining variables: file_list_1=$(echo "list.txt") or file_list_2=$(find ./) I would have used: file_list_1="list.txt" and ...
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) … ...
5
votes
3answers
6k views

How to preserve the newline 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 ...
3
votes
4answers
452 views

what is the difference between `find .` and /home/user/* as an input to for command

What is the difference between find . and /home/user/* as an input to for command.? For example: for var in $(find .) do echo "$var" done or for var in /home/user/* do echo "$var" done In the ...
3
votes
1answer
58 views

I need to create file with name is the result of the command in Solaris

I need to create only one file with name is the result of the command ls -l | head -2. I can't use environment variables. The filename must contain newline symbols. I've tried to use touch 'ls -l | ...
8
votes
3answers
4k views

Can command substitution be nested in variable substitution?

I would like to use variable substitution on a particular string that I access via a command. For example, if I copy something into my clipboard, I can access it like this. $ xclip -o -selection ...
5
votes
5answers
2k views

How to deal with spaces in a variable

I'm working on some scripting: for x in `find ./ -name *.pdf` do echo pathname $x done My filenames are Test1 ( Volume II), Test2 ( Volume II). I'm getting a return of pathname Test1 pathname ( ...
3
votes
1answer
291 views

Why is shell treating a part of the output of $(<file) as a command?

I saw this line while reading a blog on IFS that is : for i in $(<test.txt) And thought that $(<test.txt) prints the file contents to STDOUT. I maybe wrong in this, but out of curiosity I ...
3
votes
3answers
23k views

How do I suppress stderr warning messages from a command inside command substitution?

touch ~/deleted/$(echo "directory_"$(readlink -f foo)|tr '/' '\') 2> /dev/null where foo is a directory It's reading the full pathname of foo and creating it as a file replacing all '/' with '\' ...
1
vote
4answers
195 views

How to make bash substitution $(<“filename”) silent

I want to replace cat: var=$(cat "filename" 2>/dev/null) by bashism syntax: var=$(<"filename") The problem is that I don't know how to make the bashism silent to avoid such warnings: bash: ...
1
vote
1answer
519 views

Storing output of shell script to variable

I have read many threads on this topic, but none of the solutions have worked for me. I am trying to do the following: RELEASE_COMMAND_OUTPUT=$(exec ~/temp/execs/github-release release --user ...
0
votes
2answers
995 views

Makefile, sqare brackets built-in, variable expansion and command substitution

To embrace the DRY (Don’t Repeat Yourself) principle, I sometimes need to share pieces of shell commands in a Makefile. So there is a recipe somewhere in that file like: shell=/bin/bash # … .ONESHELL:...
16
votes
3answers
990 views

Any reason to use or teach `…` substitution for new development?

It is my understanding that the more modern $(...) command substitution syntax is preferred over the old `-based syntax, due to easier and less error-prone nesting and escaping syntax. Further, it ...
21
votes
1answer
723 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, ...
16
votes
5answers
22k views

Using a generated list of filenames as argument list — with spaces

I'm trying to invoke a script with a list of filenames collected by find. Nothing special, just someting like this: $ myscript `find . -name something.txt` The problem is that some of the pathnames ...
6
votes
1answer
25k views

Return code for curl used in a command substitution

I've script as below :- (edited) httpUrl="http://www.nnin.com" rep=$(curl -v -X POST -d "UID=username&PWD=pass" $httpUrl) status=$? if [ "$?" -eq 0 ]; then echo "Success" exit $status ...
8
votes
2answers
11k views

if command in find -exec

I was just trying to list all directories and files under current directory and also write if they are file or directory with the following command: find -exec echo `echo {} : ;if [ -f {} ]; then ...
10
votes
3answers
616 views

How can I generate arguments to another command via command substitution

Following on from: unexpected behaviour in shell command substitution I have a command which can take a huge list of arguments, some of which can legitimately contain spaces (and probably other ...
15
votes
7answers
5k 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 ...
5
votes
1answer
6k 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 ...
2
votes
2answers
108 views

Preventing shell from exiting when set -e is turned on?

I have set -e turned on for my script. The only thing is there is one command here that I don't want causing the script to exit if it fails, but I want everything else to do that. How can I keep set -...
1
vote
2answers
1k views

Behaviour of bash command substitution with command from string in variable

$ echo $(echo x; echo y) x y $ a='echo x; echo y' $ echo $($a) # expect 'x y' x; echo y Why does command substitution behave in such way? How to perform the command substitution for list of commands ...
2
votes
3answers
658 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 ...
2
votes
1answer
1k 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/=/ /' -...
0
votes
1answer
295 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 ...