Bash: ls, cat -n, echo: List Filenames of Current Directory

nick3499
1 min readFeb 21, 2017

--

cat_n_ls=$(ls | cat -n) assigns ls | cat -n to a variable. ls lists filenames in the current directory. Which is then passed tocat -n which numbers each line. echo "$cat_n_ls" then displays the result. "$cat_n_ls" has quotation marks to preserve newlines. The script above returns something similar to the following example:

     1 file1.sh
2 file2.sh
3 file3.sh
4 file4.sh
5 file5.sh
6 file6.sh
7 file7.sh

--

--

nick3499
nick3499

Written by nick3499

coder of JavaScript and Python

No responses yet