0

Say I have ~/scripts in my $PATH and I have script.sh inside that path.

I can execute that script by typing script.sh directly in the terminal, but what if I want to print out the full path of that script without knowing the base path of the script (or added any function inside the script to print out its own path)? Are there any good ways to do this?

10
  • which script.sh should do it Commented May 14, 2020 at 1:16
  • ~ is the symbol of your home folder
    – user1986815
    Commented May 14, 2020 at 1:17
  • 1
    In any POSIX compliant shell, avoid which, there are builtins that are well suited for that, like type , hash and command, unless which is a builtin also from the shell you're using then sure use it.
    – Jetchisel
    Commented May 14, 2020 at 1:23
  • 1
    @MaxMuster man hash to see the corresponding help page. Commented May 14, 2020 at 2:16
  • 1
    bash -c 'help hash'
    – Jetchisel
    Commented May 14, 2020 at 2:19

1 Answer 1

1

In bash, to locate a file (script) in the users path, you can use the which command: (https://ss64.com/bash/which.html), but as @Jetchisel says there are better alternatives for POSIX-compliant shells; see 'which' vs 'command -v' in Bash

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