I have written the following lines to get the last character of a string:
str=$1
i=$((${#str}-1))
echo ${str:$i:1}
It works for abcd/
:
$ bash last_ch.sh abcd/
/
It does not work for abcd*
:
$ bash last_ch.sh abcd*
array.sh assign.sh date.sh dict.sh full_path.sh last_ch.sh
It lists the files in the current folder.
Single line:
Now:
another solution using awk script:
last 1 char:
last 5 chars:
Per @perreal, quoting variables is important, but because I read this post like 5 times before finding a simpler approach to the question at hand in the comments...
Output:
/
Output:
*
Thanks to everyone who participated in this above; I've appropriately added +1's throughout the thread!
I know this is a very old thread, but no one mentioned which to me is the cleanest answer:
Note the
-n
is just so the echo doesn't include a newline at the end.Every answer so far implies the word "shell" in the question equates to Bash.
This is how one could do that in a standard Bourne shell:
is a good approach