'Create and Access Array in One Line with Bash

If I type the following command I create an array variable.

A=($(ls | sort))

Which can be printed by index by doing the following.

echo ${A[0]}

Is there any way to do both in one line? The following gives a "bad substitution" error.

echo ${($(ls | sort))[0]}

That command seems logical because it is a copy and paste of what was in the variable to where it is used. What is the issue and how can I fix it in one line without defining a variable?



Solution 1:[1]

$ A=($(ls | sort)) && echo ${A[0]}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Atisom