Assigning Variable to Line Numer in nl

hi,

i'm creating a little menu for some users.

i'm running the command:

du -a /apps | sort -n -r | head -n 10 | nl 

i then get the top 10 files by size in the /apps directory

the output is like this:

1 101415752 /apps
2 89188064 /apps/userA
3 74521335 /apps/userA/export
4 47999710 /apps/UserB

how do I assign a these outputs to variables?

so if the user selects 4, they move (cd) to /apps/UserB ??

thanks for any help

One way of doing this is using awk to extract the FS from the required line number.

Let say user selected 4 and we have the value stored in variable O

O=4
du -a /apps | sort -nr | head -n 10 | awk -v o=$O 'NR==o{print $NF}'
1 Like