首页 > 解决方案 > How to Save linux command output as list in variable

问题描述

Below command is running on linux system and I want to save output as list.

root@Linux:~ kubectl get ns | awk '{print $1}'     
NAME
b2
b6
b7
cert-manager

I need to save above command output in to variable as list.

Example:- 
NAMESPACE = ['NAME', 'b2', 'b6', 'b7', 'cert-manager']

NAMESPACE is variable

标签: linuxbashubuntu

解决方案


If the output includes only simple words you can write like this:

$ arr=( $( echo a b c d ) )
$ for i in "${arr[@]}"; do echo "$i"; done
a
b
c
d
$ arr=$( echo a b c d )
$ for i in $arr; do echo "$i"; done
a
b
c
d
$

推荐阅读