New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
option to create a variable with output #1546
Comments
Bash's printf is a builtin, which means it has some extra powers. The only
ways I know to do this from another program are to either output bash code
and eval it (scary!) Or to do something like `VAR=$(jq ...)` (less scary,
but only sets one variable at a time.)
…On Sun, Dec 3, 2017, 19:48 shawn ***@***.***> wrote:
Bash printf has a -v option to create a variable with the output. Just to
show the impact to performance this has:
***@***.*** ~]$ time for i in {0..1000}; do unset var; var="$(echo
"foo")"; done
real 0m1.212s
user 0m0.458s
sys 0m0.843s
***@***.*** ~]$ time for i in {0..1000}; do unset var; printf -v
var "foo"; done
real 0m0.008s
user 0m0.008s
sys 0m0.000s
I'm hoping it's possible (printf is also a builtin - not sure if this
allows more features than a normal command has access to) to allow a
command to declare a variable and be able to execute much faster in a
script vs needing to run in a subshell?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#1546>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/ADQ4V8BaX9ejP1ss9nsnjsopsTQtNvrlks5s80FLgaJpZM4Qz-lN>
.
|
For multiple values, consider
(This is known to work with bash 4.2) See also |
That is a really neat builtin I've never seen before!
…On Mon, Dec 4, 2017, 09:32 pkoppstein ***@***.***> wrote:
For multiple values, consider mapfile, e.g.
$ mapfile -t aname <<< $(jq -c '.[]' <<< '[[1,2],{"a":3}]')
$ echo ${#aname[@]}
2
See also How can a stream of JSON texts produced by jq be converted into
a bash array of corresponding values? at the jq FAQ
<https://github.com/stedolan/jq/wiki/FAQ>
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#1546 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ADQ4V210vTTZBSKqTzTZZMTp57MnxHQRks5s9AJrgaJpZM4Qz-lN>
.
|
Yeah, that's the same as a while read loop (unless you use it for preprocessing) as that seems to be a common question (when I was googling to see if my question was answered) it may be worth adding to jq's docs. However, it doesn't get around the speed hit of calling a subshell. it's also worth noting that mapfile is the same as readarray and is only in bash 4.2 (iirc - not default bash on OSX or RHEL 6.5 anyway). My use case is: declare JQ_STR So the expectation is that that etl function is called multiple times for different functionality (I'm already calling it 3x times after a day - it'll be called much more). |
Since jq is not a shell-builtin, it can't behave like printf. The canonical way to extract data from JSON into the shell using jq is to use the |
@sh ? (y'all can tag and close this if you want - or i'll close it in a few days - thanks) |
Bash printf has a -v option to create a variable with the output. Just to show the impact to performance this has:
swilson@localhost ~]$ time for i in {0..1000}; do unset var; var="$(echo "foo")"; done
real 0m1.212s
user 0m0.458s
sys 0m0.843s
[swilson@localhost ~]$ time for i in {0..1000}; do unset var; printf -v var "foo"; done
real 0m0.008s
user 0m0.008s
sys 0m0.000s
I'm hoping it's possible (printf is also a builtin - not sure if this allows more features than a normal command has access to) to allow a command to declare a variable and be able to execute much faster in a script vs needing to run in a subshell?
The text was updated successfully, but these errors were encountered: