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.

Command substitution is a facility that allows a command to be run and its output to be pasted back on the command line as arguments to another command.

Examples:

vi `fgrep -l malloc *.c`
vi $(fgrep -l malloc *.c)

Read more:

388 questions
288
votes
6 answers

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 same thing, or are there differences?
tshepang
  • 64,472
  • 86
  • 223
  • 290
221
votes
7 answers

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 that 'rate' (I guess pipe will be useful…
Paul
  • 2,211
  • 2
  • 12
  • 3
210
votes
6 answers

How can I execute `date` inside of a crontab 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 Unfortunately I get this message when that…
cwd
  • 44,479
  • 71
  • 146
  • 167
199
votes
3 answers

Quoting within $(command substitution) in Bash

In my Bash environment I use variables containing spaces, and I use these variables within command substitution. What is the correct way to quote my variables? And how should I do it if these are nested? DIRNAME=$(dirname "$FILE") or do I quote…
CousinCocaine
  • 2,155
  • 3
  • 13
  • 12
74
votes
8 answers

Assigning exit code to a shell local variable

#!/bin/bash function0() { local t1=$(exit 1) echo $t1 } function0 echo prints empty value. I expected: 1 Why doesn't t1 variable get assigned the exit command's return value - 1?
user93868
64
votes
5 answers

Why do newline characters get lost when using command substitution?

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 but since I am learning, I thought to use a for…
user3138373
  • 2,441
  • 6
  • 29
  • 44
57
votes
4 answers

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.
AVS
  • 773
  • 1
  • 5
  • 10
53
votes
3 answers

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? seems to say that ` evaluates the commands within and…
coolcric
  • 641
  • 1
  • 5
  • 4
48
votes
2 answers

How do I diff the outputs of two commands?

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)
KALAI SELVAN
  • 811
  • 2
  • 10
  • 11
44
votes
2 answers

nested double quotes in assignment with command substitution

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 puzzled by the nested double-quotes. As far as I can tell,…
kjo
  • 14,779
  • 25
  • 69
  • 109
36
votes
5 answers

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 https://get.docker.com/)" They appear to be…
Sarke
  • 487
  • 1
  • 4
  • 6
35
votes
1 answer

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

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 variable to the output of the command. Is it possible…
Questionmark
  • 3,885
  • 8
  • 37
  • 57
33
votes
6 answers

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 like AAA B C DDD FOO BAR by which I only mean it…
Old Pro
  • 1,272
  • 3
  • 10
  • 20
31
votes
4 answers

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 is found in the files, the following does not search…
dotancohen
  • 15,494
  • 26
  • 80
  • 116
29
votes
5 answers

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 # Output is: 610a62 # The trailing newline from the…
Peter.O
  • 32,426
  • 28
  • 115
  • 163
1
2 3
25 26