I want make a bash script which returns the position of an element from an array by give an arg. See code below, I use:
#!/bin/bash
args=("$@")
echo ${args[0]}
test_array=('AA' 'BB' 'CC' 'DD' 'EE')
echo $test_array
elem_array=${#test_array[@]}
for args in $test_array
do
echo
done
Finally I should have output like:
$script.sh DD
4
Trying to avoid complex tools:
However, it consumes 2 processes. If we allow ourselves
sed, we could do it with a single process:Update:
As pointed out by thkala in the comments, this solution is broken in 3 cases. Be careful not to use it if: