Skip to content
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

Closed
ag4ve opened this issue Dec 4, 2017 · 6 comments
Closed

option to create a variable with output #1546

ag4ve opened this issue Dec 4, 2017 · 6 comments
Labels

Comments

@ag4ve
Copy link

ag4ve commented Dec 4, 2017

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?

@wtlangford
Copy link
Contributor

wtlangford commented Dec 4, 2017 via email

@pkoppstein
Copy link
Contributor

pkoppstein commented Dec 4, 2017

For multiple values, consider mapfile, e.g.

$ mapfile -t aname <<< $(jq -c '.[]'  <<< '[[1,2],{"a":3}]')
$ echo ${#aname[@]}
2

(This is known to work with bash 4.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

@wtlangford
Copy link
Contributor

wtlangford commented Dec 4, 2017 via email

@ag4ve
Copy link
Author

ag4ve commented Dec 5, 2017

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
jq_etl () {
JQ_STR="$(jq "$@" <<< "$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).

@nicowilliams
Copy link
Contributor

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 format and eval an assignment.

@ag4ve
Copy link
Author

ag4ve commented Dec 9, 2017

@sh ?

(y'all can tag and close this if you want - or i'll close it in a few days - thanks)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants