• Categories
    Category
  • Categories
    Category
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial Comments FAQ Related Articles

How to create Bash Script using Array on Debian 12

  • 00:31 cat /etc/os-release
  • 00:43 vim array
  • 10:43 chmod +x array
  • 10:53 ./array
  • 11:25 ./array
7876

To Create Bash Script Using Array On Debian 12

Introduction

A Bash array is a variable that can hold multiple values, similar to arrays in other programming languages that allows you to store a collection of data elements under a single variable name. Arrays in Bash can be indexed and accessed using integers, and they are particularly helpful for organizing and manipulating sets of related data within shell scripts.

Procedure Steps

Step 1: Check the OS version by using the below command.

root@linuxhelp:~# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

Step 2: Make a script by using the below command

root@linuxhelp:~# vim array
#!/bin/bash

# Create request for user creation to register
echo "The is the Process for Regitering"
read -p "what is your name: " name

# Create array with sports name

sport[0]="Cricket"
sport[1]="Footbal"
sport[2]="Chess"
sport[3]="Carrom"
sport[4]="Valleybal"

array_length=${#sport[@]} # To Store length of array
index=$(($RANDOM % $array_length)) # To Increase the array Randomly

# Ask to user for creation
echo "HI!" $name, "would you like to play ${sport[$index]}" "?(yes/no)" by default:yes # To Ask user's sport option
read -p "Answer: " answer # To get answer from user for sport

if [ "$answer" == "no" ] # If the answer is no
then 
	echo "You were not chose ${sport[$index]}" # It will print
else
	echo "You were chose ${sport[$index]}" # It will pring
fi

Step 3: make the executable permission by using the below command

root@linuxhelp:~# chmod +x array 

Step 4: Run the script by using the below command

root@linuxhelp:~# ./array 
The is the Process for Regitering
what is your name: linuxhelp1
HI! linuxhelp1, would you like to play Footbal ?(yes/no) by default:yes
Answer: yes
You were chose Footbal

Step 5: Run the script by using the below command

root@linuxhelp:~# ./array 
The is the Process for Regitering
what is your name: linuxhelp2
HI! linuxhelp2, would you like to play Chess ?(yes/no) by default:yes
Answer: no
You were not chose Chess

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to create Bash Script using Array on Debian 12. Your feedback is much welcome.

Tags:
caden
Author: 

Comments ( 0 )

No comments available

Add a comment

Frequently asked questions ( 5 )

Q

What is a Bash Array?

A

A Bash Array is a variable that can store multiple values under a single name. It allows you to manage collections of data in shell scripts.

Q

How do you access elements in a Bash Array?

A

You can access elements in a Bash array using their index (starting from 0). For example, to access the first element of my_array, use ${my_array[0]}.

Q

Can Bash Arrays have associative (key-value) elements?

A

Yes, Bash supports associative arrays where elements are indexed by keys instead of integers. They are declared with declare -A array_name and accessed using ${array_name[key]}.

Q

How do you check if an element exists in a Bash Array?

A

You can iterate through the array and check each element, or use a loop with conditional statements to check for specific values.

Q

Are Bash Arrays passed by reference or by value?

A

Bash arrays are passed by reference, meaning changes made to an array in a function will affect the original array outside the function.

Related Tutorials in How to create Bash Script using Array on Debian 12

Related Tutorials in How to create Bash Script using Array on Debian 12

How to install Gparted on Debian 9.0
How to install Gparted on Debian 9.0
Sep 13, 2017
Installation SSL Certificate on Ubuntu/Linuxmint/Debian to Secure Apache
Installation SSL Certificate on Ubuntu/Linuxmint/Debian to Secure Apache
Sep 19, 2018
How to install and update OpenSSL on Debian 11.3
How to install and update OpenSSL on Debian 11.3
Oct 21, 2022
How to install qBittorrent on Debian 9.0
How to install qBittorrent on Debian 9.0
Sep 8, 2017
How to Install FileZilla in Debian
How to Install FileZilla in Debian
Nov 29, 2016
How to install Nmap on Debian 9.0
How to install Nmap on Debian 9.0
Sep 9, 2017
How to Install Laravel in Debian
How to Install Laravel in Debian
Dec 7, 2016
How to install Wireshark in Debian
How to install Wireshark in Debian
Mar 18, 2017

Related Forums in How to create Bash Script using Array on Debian 12

Related Forums in How to create Bash Script using Array on Debian 12

MariaDB
ryan class=
E: Unable to locate package mariadb-server
Sep 18, 2017
Linux
AadrikaAnshu class=
How to add timestamps to history On Any Linux Machine
Jun 18, 2019
Shell Script
landon class=
How to add multiple user in samba using shell script
Oct 25, 2017
vim
jacob class=
Change true vi-compatible editor from Debian Vim-compatible
Nov 8, 2021
debian
anandaamatya class=
RedNotebook on Debian buster
Jun 30, 2020
Bash-scripting
aristosv class=
Bash script filtering output issue
Jun 4, 2019
Shell Script
symonds class=
Sort only unique words inside a file
Apr 6, 2018
Ubuntu
Kirin class=
Videos Always Micro-Shutter
Mar 8, 2019

Related News in How to create Bash Script using Array on Debian 12

Related News in How to create Bash Script using Array on Debian 12

Debian IceDove kicks the bucket after Thunderbird revisits Debian Repositories
Debian IceDove kicks the bucket after Thunderbird revisits Debian Repositories
Feb 28, 2017
Mass update of Jessie - A better alternative to new version?
Mass update of Jessie - A better alternative to new version?
May 8, 2017
Debian 9.2 ‘Stretch’ OS is here, download distro now
Debian 9.2 ‘Stretch’ OS is here, download distro now
Oct 9, 2017
Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Luke ?
workbench for debian

I am using workbench in CentOS whereas now I need to use Debian Operating system so could you please help to install and use in Debian?

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.