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 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. 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' 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.