I am new to Bash Scripting and was working with arrays when the tutorial I was referring to stated that command line arguments could be turned to an array using "$@" so i wrote a simple script that would input two command line arguments into an array and print them
script1.sh
#!/bin/bash
array=("$@")
echo "whole array : $@"
echo "arr[0]: ${array[0]}"
echo "arr[1]: ${array[1]}"
but when i run it as
./script1.sh hello world
it gives output as
it puts all arguments in array[0], i do not understand.. some stack overflow articles comes close to this issue I am using Kali Linux Downloaded from windows store with Windows Subsystem for linux turned on , if that should be of any help.....
EDIT :
I created a small GIF and uploaded it publicly on gifyu to show the execution and prove that i used the same code! link : https://gifyu.com/image/eqvT
added $BASH_VERSION as a user pointed out in comments...
EDIT 2:
output of locale
DEBUG
EDIT 3: I am Finally able to Run the Script as desired by using
#!/bin/bash
echo Bash Version: $BASH_VERSION
str="$@"
read -r -a array <<< "$str"
echo "array[0]:" ${array[0]}
echo "array[1]:" ${array[1]}
echo "array[2]:" ${array[2]}
But it would be helpful if i knew why $@ was not working....
and also the automatic Word Splitting doesn't work for example if I change the code to
str="$@"
array=( $str )
then output is given as
EDIT 4: Well it works flawlessly on Ubuntu 20.04 LTS
Output :
I think there is some problem with kali WSL OR my there is some environmental or Configuration problem in my PC.