0

Special parameter "$@" contains the following string variables,

echo $@ outputs: a.bdf, b.bdf,c.nas,d.nas

I want to extract the string variables with extension 'bdf' and save it in another array. Is it possible to do so in bash?

2

3 Answers 3

1

Just iterate through it with a for loop :

for ARG in "$@";do
    if [[ "$ARG" == *.bdf ]];then
        BDF_ARRAY+=("$ARG")    #you don't need to initialize this array before the loop in bash
    else                       #optional block, if you want to split $@ in 2 arrays
        OTHER_ARRAY+=("$ARG")  
    fi
done

echo ${BDF_ARRAY[@]}
echo ${OTHER_ARRAY[@]}
Sign up to request clarification or add additional context in comments.

Comments

0

Using for loop

for i in "$@";do
    if [[ "${i##*.}" == bdf ]]; then
        ARRAY2+=("$i")
    fi
done

Comments

0

In the generic case:

#!/bin/bash

for i in "$@"; do
    case "$i" in
        *.bdf)  BDF_ARRAY+=("$i")
                ;;
        *.nas)  NAS_ARRAY+=("$i")
                ;;
    esac
done

for i in "${BDF_ARRAY[@]}"; do echo "BDF: $i"; done
for i in "${NAS_ARRAY[@]}"; do echo "NAS: $i"; done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.