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
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 ...
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 ...
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 ...
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 ...
36
votes
4answers
73k views

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?
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? ...
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 ...
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 ...
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 # ...
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 ...
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, ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
12
votes
4answers
41k 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.
12
votes
2answers
6k views

Nested quotes in subshells

Say I have to use quotes to encapsulate subshell output like: DATA="$(cat file.hex | xxd -r)" But I need to nest this kind of stuff like: DATA="$(cat file.hex | xxd -r | tr -d \"$(cat trim.txt)\")" ...
12
votes
1answer
524 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 ...
12
votes
3answers
19k views

Multivariable For Loops

Is there a way to specify multiple variables (not just integers) in for loops in bash? I may have 2 files containing arbitrary text that i would need to work with. What i functionally need is ...
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 ...
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
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 ...
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) || ...
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 ...
9
votes
1answer
670 views

When printing a variable that contains newlines, why is the last newline stripped? [duplicate]

Contents of file.txt (no weirdness, text file as defined by POSIX) iguana gecko anole Sample script: #!/bin/sh string="$(cat file.txt)" printf '%s' "$string" Sample output: [coolguy@somemachine ...
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 ...
9
votes
1answer
257 views

Why there isn't a new line at the end of quoting a subshell and passing the results to echo?

When I do ls | grep png the output of grep is: 2015-05-15-200203_1920x1080_scrot.png 2015-05-16-025536_1920x1080_scrot.png (filename,newline,filename,newline) then, echo $(ls | grep png) outputs: ...
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 ...
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 ...
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 ...
7
votes
1answer
828 views

exit not terminating the script [duplicate]

exit doesn't terminate the script when error is called.. output Error: Could not resolve localhost after exit script #!/bin/sh resolve_ip (){ if [ -z "$1" ]; then host="localhost" ...
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 ...
7
votes
3answers
1k views

Special character '#' in Perl SSH command

I try to send command using SSH in my perl script with # but it gets truncated right at # Example: Input text is : $message = "Product ID # STK000134" The SSH command is : $execute=`ssh -q id@...
7
votes
3answers
16k views

Split command output by linebreak?

I have a command returning multiple lines. For further processing I need to process each single line of those lines. My current code works by modifying the IFS (Internal Field Separator): ROWS=$(...
7
votes
4answers
2k views

Run a Command and then Parameter Substitution in one line

bash# hostname host1.example.com I only want host1. So: SHORT_HOST=$(/bin/hostname) SHORT_HOST=${SHORT_HOST%%.*} Can I turn this into a one liner? - or - what is the best way to make $SHORT_HOST ...
7
votes
2answers
890 views

Elegant way to prevent command substitution from removing trailing newline

I'm customizing my zsh PROMPT and calling a function that may or may not echo a string based on the state of an environment variable: function my_info { [[ -n "$ENV_VAR"]] && echo "Some ...
7
votes
2answers
571 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.
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 ...
6
votes
1answer
29k views

Bash: Merge foldername from variable with filename

First I write a configfile with all my parameters like this path="/home/test/" I name it test.conf. Then I write a shell script with this content, name it test, and make it executable with chmod +x....
6
votes
2answers
16k 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?
6
votes
3answers
2k 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/...
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) … ...
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 ...
6
votes
2answers
2k views

Word-splitting when parameter is used within command substitution

What needs to be done to ensure that a parameter containing embedded spaces is handled correctly when used within a command substitution? Perhaps an example will illustrate $ file="/home/1_cr/xy z" $ ...
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 ...
6
votes
2answers
799 views

“command | less” vs “less <(command)”

A common "idiom" for viewing large amounts of command output is to pipe it to less, via command | less. However, it's also possible (perhaps only in bash, haven't tested in other shells) to use the ...