-3

I am trying to use ls , cat etc commands inside a bash function

Report()
{
    ls $1 |  sort -u
}
Report()

I am getting ls: command not found error when I run the function

Is it not possible to use bash commands inside bash function?

I could see examples where only echo is used inside the function

5
  • 4
    When calling a function in bash, you shouldn't use the parentheses. They're used only in its definition.
    – choroba
    Commented Oct 12, 2023 at 7:14
  • 1
    How do you run the script? What's the contents of $PATH when it runs?
    – choroba
    Commented Oct 12, 2023 at 7:16
  • Sorry added braces while calling function, if I pass path as an argument? Report() { ls $1 | sort -u } Report "$PATH" or Report() { ls ../../../ | sort -u } Report Ls or sort , none are working for me in function Commented Oct 12, 2023 at 8:05
  • @pratheekshalp Did you assign something to PATH? If so, you probably broke it. See this question, especially tripleee's answer. Commented Oct 12, 2023 at 8:09
  • 1
    Report(){ ls "${1:-.}" | sort -u; }; Report;
    – jhnc
    Commented Oct 12, 2023 at 10:49

1 Answer 1

0

When calling a function in bash, you shouldn't use the parentheses

Report()
{
    ls $1 |  sort -u
};
Report;
2
  • The semicolons are superfluous.
    – Biffen
    Commented Oct 13, 2023 at 10:27
  • You should quote variables so that their content is not mangled (glob expansion, wordsplitting, etc. eg. Report '/path/with space and *'). In this case, something more sophisticated than simple quoting is needed, since it appears not passing the parameter at all is desired to be supported.
    – jhnc
    Commented Oct 14, 2023 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.