command-substitution

Bash Command Substitution with Variables

痞子三分冷 提交于 2019-12-08 06:13:06
问题 I have a bash script that builds a command based on a list of files so the command is built on the fly. Building it on the fly means it gets stored in a variable. I then want to run that command and store the output in a separate variable. When I use command substitution to try and run the command, it craps out. How can I get command substitution to work with a command in a variable when that variable utilizes pipes? Here is my script: # Finds number of files that are over 365 days old

Exit code of command substitution in bash local variable assignment [duplicate]

前提是你 提交于 2019-12-04 15:19:03
问题 This question already has answers here : Why does “local” sweep the return code of a command? (2 answers) Closed 3 years ago . How can I check the exit code of a command substitution in bash if the assignment is to a local variable in a function? Please see the following examples. The second one is where I want to check the exit code. Does someone have a good work-around or correct solution for this? $ function testing { test="$(return 1)"; echo $?; }; testing 1 $ function testing { local

Do escaped double quotes in command substitution become literals in Bash?

徘徊边缘 提交于 2019-12-01 09:47:20
问题 If I execute this in Bash echo "1 2" I get 1 2 . But if I execute echo \"1 2\" I get "1 2" . Now I would figure if I execute echo $(echo \"1 2\") I would get 1 2 . But again, I get "1 2" . In fact, no matter how many command substitutions in the chain echo $(echo $( ... echo \"1 2\") ... ) I always get "1 2" . Why is that? 回答1: After substituting the output of $(command) back into the command line, the only additional parsing that's done is word-splitting and wildcard expansion. Quotes are

Command substitution vs process substitution

心已入冬 提交于 2019-11-30 06:35:13
问题 I am trying to understand the differences between these two similar commands. aa=$(foo | bar | head -1) read aa < <(foo | bar | head -1) I know that <() requires #!/bin/bash , but does that make it slower? Do they create the same amount of subshells? Do they require the same amount of bash or sh processes? I am looking to use the command with the best performance. 回答1: No. Bash is faster in non-POSIX mode. It depends. This is both an implementation-specific and platform-specific detail. In

PS1 command substitution fails when containing newlines on msys bash

邮差的信 提交于 2019-11-29 01:34:58
This command succeeds $ PS1='$(date +%s) $ ' 1391380852 $ However if I add a newline it fails $ PS1='$(date +%s)\n$ ' bash: command substitution: line 1: syntax error near unexpected token `)' bash: command substitution: line 1: `date +%s)' If I use backticks it works $ PS1='`date +%s`\n$ ' 1391381008 $ but backticks are discouraged . So what is causing this error? GNU bash, version 4.2.45(6)-release You can disambiguate the parsing easily, to prevent hitting any such bug (though I can't reproduce it myself): PS1='$(date +%s)'$'\n$ ' This $'\n' syntax parses to a literal newline character,

Command substitution with string substitution

可紊 提交于 2019-11-28 13:52:35
Is it possible to do something along the lines of: echo ${$(ls)/foo/bar} I'm pretty sure i saw somewhere working example of something like that but this results in "bad substitution" error. I know that there are other methods to do that but such a short oneliner would be useful. Am I missing something or is this impossible? Syntax ${...} only allows referencing a variable (or positional parameter), optionally combined with parameter expansion . Syntax $(...) (or, less preferably, its old-style equivalent, `...` ), performs command substitution , which allows embedding arbitrary commands to

PS1 command substitution fails when containing newlines on msys bash

帅比萌擦擦* 提交于 2019-11-27 16:03:38
问题 This command succeeds $ PS1='$(date +%s) $ ' 1391380852 $ However if I add a newline it fails $ PS1='$(date +%s)\n$ ' bash: command substitution: line 1: syntax error near unexpected token `)' bash: command substitution: line 1: `date +%s)' If I use backticks it works $ PS1='`date +%s`\n$ ' 1391381008 $ but backticks are discouraged. So what is causing this error? GNU bash, version 4.2.45(6)-release 回答1: You can disambiguate the parsing easily, to prevent hitting any such bug (though I can't

When does command substitution spawn more subshells than the same commands in isolation?

笑着哭i 提交于 2019-11-27 11:49:44
Yesterday it was suggested to me that using command substitution in bash causes an unnecessary subshell to be spawned. The advice was specific to this use case : # Extra subshell spawned foo=$(command; echo $?) # No extra subshell command foo=$? As best I can figure this appears to be correct for this use case. However, a quick search trying to verify this leads to reams of confusing and contradictory advice. It seems popular wisdom says ALL usage of command substitution will spawn a subshell. For example: The command substitution expands to the output of commands. These commands are executed

How do I assign the output of a command into an array?

て烟熏妆下的殇ゞ 提交于 2019-11-26 22:04:38
I need to assign the results from a grep to an array... for example grep -n "search term" file.txt | sed 's/:.*//' This resulted in a bunch of lines with line numbers in which the search term was found. 1 3 12 19 What's the easiest way to assign them to a bash array? If I simply assign them to a variable they become a space-separated string. jordanm To assign the output of a command to an array, you need to use a command substitution inside of an array assignment. For a general command command this looks like: arr=( $(command) ) In the example of the OP, this would read: arr=($(grep -n "search

Backticks vs braces in Bash

非 Y 不嫁゛ 提交于 2019-11-26 18:57:45
When I went to answer this question , I was going to use the ${} notation, as I've seen so many times on here that it's preferable to backticks. However, when I tried joulesFinal=${echo $joules2 \* $cpu | bc} I got the message -bash: ${echo $joules * $cpu | bc}: bad substitution but joulesFinal=`echo $joules2 \* $cpu | bc` works fine. So what other changes do I need to make? fedorqui The `` is called Command Substitution and is equivalent to $() (parenthesis), while you are using ${} (curly braces). So these are equal and mean "interpret the command placed inside": joulesFinal=`echo $joules2 \