Here is the code that shows the problem:
#!/bin/bash
function char() {
local char="$(echo $1 | cut -c2)" # Gets second character from argument.
echo $char
}
char -a # Outputs 'a'.
char -e # Or 'char -n' outputs nothing.
I want this code to output [ 'a', 'e' ] instead of [ 'a', nothing ].
I think that the problem is in echo $1. My question is kind of similar to this one. But it seems that most solutions don't work for me. I think that Bash changed it's behavior from version in that question: '3.00.15(1)-release (x86_64-redhat-linux-gnu)'.
My Bash version is '4.3.30(1)-release (i586-pc-linux-gnu)'. I have tried:
echo x-e # Outputs 'x-e'.
echo -- -e # Outputs '-- -e'.
echo "-e " # Outputs '-e'.
Only little "hack" that works is echo "$1 ". But I've got the feeling that it's not the best thing to do.
P.S. I personally find this pretty interesting. Thanks for any thoughts on this.