My script is saved as recent.sh and reads currently as:

#!/bin/bash -x
rcnt() {
    ls -t -p | grep -v / | head -n1 | cat

When I source recent.sh it and execute rcnt, it prints the most recent file in the directory. However, when I ./recent.sh or sh recent.sh it does not print anything to the Terminal. (I try to run it from the directory where it is saved.)

I am trying to run this in Terminal in Linux Mint 19 and I've already tried it with usr/bin/env bash, without the -x, with sh instead of bash. Permissions are 777.

How to run this script so that it will print to the Terminal where I executed it?

I'm a beginner, I might be overlooking something, but I read all the relevant SO, StackExchange, etc. answers which mostly recommend setting permissions right, which I believe I have right, the location of bash (which for me is bin/bash) or other code-specific issues.

  • 1
    You have to call the function you've written . Add " rcnt " in a new line. And also remove the "-x" option in the shebang. It's unnecessary. Jun 7, 2020 at 17:22
  • Thanks @ParsaMousavi! Slowly but surely I am learning that a function needs to be called in order to be executed.
    – O1G
    Jun 7, 2020 at 17:33

1 Answer 1


You have to call the function you've wrote in order to execute it.
Function calls in bash don't require parentheses. So your script should look like this :

rcnt() {           #Function definition
    ls -t -p | grep -v / | head -n1 | cat

rcnt               #Function invocation

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .