Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash function argument unexpectedly interpreted as file/directory

I created a function in my .bashrc file to print the top N files/directories in the working directory:

# .bashrc
function topN () { du -hs * | sort -rh | head -"$1"; }

However, sourcing my .bashrc and then running topN 5 returns:

head: cannot open '5' for reading: No such file or directory

I have attempted various quoting methods and combinations, with generally the same error. How can I re-write this function to treat the argument 5 as a value rather than a file/directory?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Simply running du -hs * | sort -rh | head -5 returns the expected listing of the top 5 files/directories and their sizes.

>Solution :

You were running a (stale) alias rather than the function you shared with us.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading