You need to use the $IFS. It is a special shell variable. You can change the value of IFS as per your requirements. The Internal Field Separator (IFS) that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is <space><tab><newline>. You can print it with the following command:
cat -etv
Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | GNU/Bash + Unix |
Time | N/A |
Syntax
Create a variable called ns as follows:
ns="ns1.cyberciti.biz ns2.cyberciti.biz ns3.cyberciti.biz"
To split $ns variable (string) into array, use the following IFS syntax:
OIFS="$IFS" IFS=' ' read -a dnsservers <<< "${ns}" IFS="$OIFS"
OR use one liner as follows:
IFS=' ' read -a dnsservers <<< "${ns}"
To display values stored in an array, enter:
echo ${dnsservers[0]}
Sample outputs:
ns1.cyberciti.biz
Use bash for loop to iterate through array values i.e. print all elements using bash for loop syntax:
for i in "${dnsservers[@]}" do echo "$i" done
🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 6 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
You could also do:
can u plz tell me all syntex and contitions for c , c++ program……
ns=”ns1.cyberciti.biz ns2.cyberciti.biz ns3.cyberciti.biz”
n=0
A=()
for i in $ns;do A[$n]=$i; ((n++));done
echo ${A[0]}
ns1.cyberciti.biz
echo ${A[1]}
ns2.cyberciti.biz
echo ${A[2]}
ns3.cyberciti.biz
echo ${A[3]}
ns3.cyberciti.biz
Just FYI:
“The default value is .”
That stmt got messed-up in the html-formatting of your article.
I can see in the html-code, you meant to write:
“The default value is (space)(tab)(newline).”
which is correct :)
Thanks for the heads up!
As the guy above me said, space,tab,newline are the default delimiters.
If your input string is already separated by spaces, bash will automatically put it into an array:
ex.
array=( H E L L O ) # you don’t even need quotes
array[0] $ = H
if you wanted to accept other ascii chars (say you’re converting to hex for some reason)
array=(H E L L O “#” “!” ) #some chars you’ll want to use the quotes
cat test.txt
test1 test2 test3
array=($( cat test.txt ))
array[0] = test1
array=($( ls directory/ )) #ls by default will output each dir on a new line, so each subdir or whatever ls returns becomes an array element
array[0] = /home/user
One last thing: array=( ${array[@]} “test” ) will add “test” string to an array. Say if you wanted to add the result of a bunch of different directories…
array=( ${array[@]} “`ls /home/`” “`ls /home/user`” “`ls /proc/sys/`” ) and so on, note the ` backtics ` this time.