1

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]}"

enter image description here

but when i run it as

./script1.sh hello world

it gives output as

enter image description here

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

enter image description here

DEBUG

enter image description here

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

enter image description here

EDIT 4: Well it works flawlessly on Ubuntu 20.04 LTS

enter image description here

Output :

enter image description here

I think there is some problem with kali WSL OR my there is some environmental or Configuration problem in my PC.

  • 1
    I can't reproduce that error. Also where did the `(` and `)` came from? – Jetchisel Oct 21 '21 at 05:20
  • @Jetchisel ( and ) in the output? well thats the question.. – Hrithik Raj Oct 21 '21 at 05:23
  • Yes from the output. Since there is nothing from the input about `(` and `)` – Jetchisel Oct 21 '21 at 05:28
  • @Jetchisel i seriously dont know – Hrithik Raj Oct 21 '21 at 05:32
  • Seriously, We don't know too..., Did you tried to debug? `set -x` is your friend. – Jetchisel Oct 21 '21 at 05:34
  • Not reproducible. The script you were running is most certainly not identical to the script you posted the code. Add a `echo $BASH_VERSION` to your script , and re-run it. – user1934428 Oct 21 '21 at 07:00
  • Are you sure the second line of your script isn't actually `array="($@)"`? – NotTheDr01ds Oct 21 '21 at 11:51
  • @Jetchisel those ( and ) are coming from array=("$@") part ...if I remove the parenthesis like array="$@" then ( and ) are removed from output...... – Hrithik Raj Oct 22 '21 at 08:24
  • 1
    @NotTheDr01ds see the EDIT ive put a GIF file that shows program execution – Hrithik Raj Oct 22 '21 at 08:28
  • @Jetchisel is there any other way around it ? i mean to input command line arguments as array? it would be of much help – Hrithik Raj Oct 22 '21 at 09:19
  • @NotTheDr01ds are there other ways to input Command line arguments as an array? – Hrithik Raj Oct 22 '21 at 19:15
  • @HrithikRaj You seem to be doing it right. I don't think anyone here has a good reason why it isn't working for you, so I'm hoping your question gets re-opened. Note that I tried this as well on WSL2 with Kali and Bash, and I was also *not* able to reproduce. It prints "Hello" and "World" as the two items without parenthesis for me. – NotTheDr01ds Oct 22 '21 at 19:53
  • @HrithikRaj Any progress on this? You might try as an earlier comment suggested and add `set -x` to the beginning of your script. Also, just grasping here, but what is the output of `locale`? – NotTheDr01ds Nov 01 '21 at 19:47
  • @NotTheDr01ds see the new EDIT i've put output for locale and debug info...... those ( and ) are coming from array=("$@") part ...if I remove the parenthesis like array="$@" then ( and ) are removed from output it seems that array variable is interpreted as some kind of a string – Hrithik Raj Nov 03 '21 at 08:41
  • @NotTheDr01ds i've been trying different combinations of commands like removing all paranthesis and other techniques found while searching multiple forums for this.... for now i've skipped the $@ part from the tutorial and resorted to using $1 - $9 ...... it would have been easy if there was other way around it.... – Hrithik Raj Nov 03 '21 at 08:49
  • Do not convert text content into pixels. And [try it online](https://tio.run/##S0oszvj/X1lRPykzTz8JyOFKLCpKrLTVUFJxUNLkSk3OyFdQKs/Iz0lVAEsoWCkAJaDiQJFog1igSDVYDsiuRZYyRJIyBEr9//8/IzUnJ/9/eX5RTgoA) to understand, why the question has been closed. – ceving Nov 03 '21 at 09:28
  • @HrithikRaj Well that's good. I was wondering about the `read -a` option, but I hadn't been able to get the syntax quite right myself, so I hadn't suggested it. Here's an additional oddity -- I would expect the `bash -x` stack trace to start each line with `+`, but instead it is using `'`. I can't find any documentation or example which shows bash stack traces starting with `'`, but I'm not all that experienced with the feature, so I could be missing something obvious. – NotTheDr01ds Nov 03 '21 at 16:09
  • 1
    @HrithikRaj Also wondering about just trying in a new WSL instance to see if we can eliminate anything environmental in the Kali instance. You might try installing Ubuntu and see if you have any differences? – NotTheDr01ds Nov 03 '21 at 16:11
  • @HrithikRaj And, while I understand why you posted the images (people didn't believe your input/output), I would recommend converting most of those into code blocks. WSL makes it really easy to get the output of a command onto the clipboard -- For instance, `./script1.sh hello world hrithik | clip.exe` – NotTheDr01ds Nov 03 '21 at 16:16
  • @HrithikRaj One more question/suggestion -- What's your terminal? If it's the "normal" Windows Console, try installing Windows Terminal (or Visual Studio Code) and trying there. – NotTheDr01ds Nov 03 '21 at 16:17
  • @NotTheDr01ds see EDIT 4...i am convinced its a problem with my PC – Hrithik Raj Nov 03 '21 at 18:29
  • @HrithikRaj Well now we are getting somewhere. Not a problem with your PC apparently, but something going on in that Kali WSL instance, right? Try this -- Close down the Kali instance and then restart it using `wsl ~ -d kali-linux -e bash --noprofile --norc` and then try your script again there. – NotTheDr01ds Nov 03 '21 at 20:09
  • @HrithikRaj But also, stop posting images. I know it's already been closed, but it gets bumped each time you edit it, and you are just inviting downvotes when you post a lot of images where text will work :-). – NotTheDr01ds Nov 03 '21 at 20:10

0 Answers0