3
nmcli -t -f STATE,WIFI,WWAN

gives the output

connected:enabled:disabled

which I'd like to convert to something like

Networking: connected, Wifi: enabled, WWAN: disabled

The logical solution to me is to turn this into an array. Being quite new to bash scripting, I have read that arrays are just regular variables and the elements are separated by whitespace. Currently my code is

declare -a NMOUT=$(nmcli -t -f STATE,WIFI,WWAN nm | tr ":" "\n")

which seems to sort of work for a for loop, but not if i want to ask for a specific element, as in ${NMOUT[]}. Clearly I am missing out on some key concept here. How do I access specific elements in this array?

2 Answers 2

2
IFS=: read -a NMOUT < <(nmcli -t -f STATE,WIFI,WWAN)
Sign up to request clarification or add additional context in comments.

Comments

1

Ignacio Vazquez-Abrams provided a much better solution for creating the array. I will address the posted question.

Array's in bash are indexed by integers starting at 0.

"${NMOUT[0]}" # first element of the array
"${NMOUT[2]}" # third element of the array
"${NMOUT[@]}" # All array elements
"${NMOUT[*]}" # All array elements as a string

The following provides good information on using arrays in bash: http://mywiki.wooledge.org/BashFAQ/005

3 Comments

thanks for the link; looks useful! i ended up giving up after finding another command whose output was already formatted the way i needed, but this will be useful to know in the future.
@dn3s The best way to say "thanks" is by upvoting or accepting answers.
sorry about that. accepted, but can't upvote yet due to lack of rep.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.