88
votes
Accepted
Assigning exit code to a shell local variable
local t1=$(exit 1) tells the shell to:
run exit 1 in a subshell;
store its output (as in, the text it outputs to standard output) in a variable t1, local to the function.
It's thus normal that t1 ...
86
votes
Pass the output of previous command to next as an argument
I tend to use this:
command1 | xargs -I{} command2 {}
Pass output of command1 through xargs using substitution (the braces) to command2. If command1 is find be sure to use -print0 and add -0 to ...
63
votes
Accepted
How do I diff the outputs of two commands?
Use process substitution:
diff <(cat /etc/passwd) <(cut -f2 /etc/passwd)
<(...) is called process substitution. It converts the output of a command into a file-like object that diff can ...
57
votes
Why do newline characters get lost when using command substitution?
Newlines get swapped out at some points because they are special characters. In order to keep them, you need to make sure they're always interpreted, by using quotes:
$ a="$(cat links.txt)"
$ echo "$...
53
votes
Accepted
What's the difference between `curl | sh` and `sh -c "$(curl)"`?
There is a practical difference.
curl -sSL https://get.docker.com/ | sh starts curl and sh at the same time, connecting the output of curl with the input of sh. curl will carry out with the download (...
52
votes
Accepted
Bash multiplication and addition
Using arithmetic expansion:
for (( k = 0; k < 50; ++k )); do
a=$(( 2*k + 1 ))
echo "$a"
done
Using the antiquated expr utility:
for (( k = 0; k < 50; ++k )); do
a=$( expr 2 '*' "$k" + 1 ...
51
votes
Accepted
What's the right way to quote $(command $arg)?
You need to use "$(somecmd "$file")".
Without the quotes, a path with a space will be split in the argument to somecmd, and it will target the wrong file. So you need quotes on the inside.
Any ...
46
votes
The old ticks vs parentheses issue: confused
The Bash FAQ gives a number of reasons to prefer parentheses to backticks, but there isn’t a universal rule that you shouldn’t ever use backticks.
The main reason to prefer parentheses in my view is ...
41
votes
Accepted
Why do newline characters get lost when using command substitution?
The newlines were lost, because the shell had performed field splitting after command substitution.
In POSIX Command Substitution section:
The shell shall expand the command substitution by ...
41
votes
Accepted
Can I get the exit code from a sub shell launched with $(command)?
Yes, it is possible without even getting too far out of your way:
$ $(exit 3); echo $?
3
$ foo="$(echo bar; exit 3)"; echo $?; echo $foo
3
bar
37
votes
nested double quotes in assignment with command substitution
Once one is inside $(...), quoting starts all over from scratch.
In other words, "..." and $(...) can nest within each other. Command substitution, $(...), can contain one or more complete ...
35
votes
Accepted
grep files from list
You seem to be grepping the list of filenames, not the files themselves. <(cat files.txt) just lists the files. Try <(cat $(cat files.txt)) to actually concatenate them and search them as a ...
29
votes
Accepted
Return code for curl used in a command substitution
Capturing exit status of commands
The assignment of command output to the rep variable does not lose the exit status of the curl command; it is still available as $?. For more details, see How can I ...
29
votes
Nested quotes in subshells
You don't need to escape the quotes inside a subshell, since the current shell doesn't interpret them (it doesn't interpret anything from $( to ), actually), and the subshell doesn't know about any ...
27
votes
Accepted
Difference of using () and $() to execute a series of commands
Okay, let's break this down. A subshell executes its contents in a chain (i.e., it groups them). This actually makes intuitive sense as a subshell is created simply by surrounding the chain of ...
25
votes
Accepted
Why there is such a difference in execution time of echo and cat?
There are several things to consider here.
i=`cat input`
can be expensive and there's a lot of variations between shells.
That's a feature called command substitution. The idea is to store the ...
25
votes
Accepted
What is command substitution in a shell?
"Command substitution" is the name of the feature of the shell language that allows you to execute a command and have the output of that command replace (substitute) the text of the command.
...
25
votes
Accepted
When printing a variable that contains newlines, why is the last newline stripped?
It's not the printing, it's the command substitution that does that. It's defined to do that. From the POSIX description:
The shell shall expand the command substitution by executing command in a ...
23
votes
Understanding Bash's Read-a-File Command Substitution
$(<file) (also works with `<file`) is a special operator of the Korn shell copied by zsh and bash. It does look a lot like command substitution but it's not really.
In POSIX shells, a simple ...
22
votes
Accepted
Why won't function return until background process ends?
The function returns, but the command substitution blocks, because you created a background job, but you still have your stdout fd opened. Just close it by adding >/dev/null before the &.
#!/...
22
votes
Accepted
How is the return status of a variable assignment determined?
It is documented (for POSIX) in Section 2.9.1 Simple Commands
of The Open Group Base Specifications.
There's a wall of text there; I direct your attention to the last paragraph:
If there is a ...
22
votes
20
votes
The old ticks vs parentheses issue: confused
Personally, I would use $(...), always, just because it's more consistent in the corner cases of nested quotes and expansions, and I like the idea of having expansions start consistently with a dollar ...
19
votes
Accepted
exit not terminating the script
exit exits the current shell process¹.
In $(resolve_ip), resolve_ip is running in a subshell process.
You can do:
my_ip=$(resolve_ip) || exit
master_ip=$(resolve_ip "$hostname") || exit
if [ "$...
18
votes
Accepted
Any reason to use or teach `…` substitution for new development?
Since back-ticks are often used, it makes sense to teach this syntactic construct.
Of course, $() style command substitution should be emphasized as the default style (and standard conforming ...
18
votes
Accepted
nested double quotes in assignment with command substitution
Your puzzle isn't right about how bash (and the shell in general) parsed the input. In:
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
First, bash parse the right hand side of ...
18
votes
Accepted
How do I ignore "command substitution: ignored null byte in input"?
Strings in bash can not contain a NUL byte, and that includes any output from a command substitution. Bash variables can't contain a NUL either. This can not be ignored or over-ridden (although it can ...
17
votes
How can I execute `date` inside of a cron tab job?
If you would like to make the date formatting string as a variable (to avoid duplicating the whole string), DO NOT escape % and DO NOT put it in $()
For example, while declare the string, just write:
...
17
votes
Accepted
Why is shell treating a part of the output of $(<file) as a command?
The $(command) syntax executes command in a subshell environment and replaces itself with the standard output of command. And, as Bash Manual says, $(< file) is just a faster equivalent of $(cat ...
17
votes
How do I ignore "command substitution: ignored null byte in input"?
I believe the echo is the problem and is unnecessay.
Consider this line instead.
python3 -c 'print("Test"+"\0"+"M"*18)' | nc -u [IP] [PORT]
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
command-substitution × 371bash × 218
shell × 116
shell-script × 75
quoting × 49
command-line × 25
pipe × 22
find × 20
variable × 20
variable-substitution × 15
io-redirection × 14
zsh × 13
echo × 11
grep × 9
linux × 8
ssh × 8
sed × 8
here-document × 8
subshell × 8
alias × 7
exit-status × 7
text-processing × 6
wildcards × 6
ksh × 6
array × 6