Finding location of command executed [duplicate] - bash

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Can a Bash script tell what directory it's stored in?
Is there a command-line function (bash) to find the location of the command you've executed?
I understand I could check my $PATH for things I've imported manually but there must be some way to do this. A tricky echo command or something?

Besides whereis, see also which and type.

Related

How to see bash script executed command? [duplicate]

This question already has answers here:
How can I debug a Bash script? [closed]
(12 answers)
Closed 2 years ago.
I'm not sure I address this problem correctly, but I've search and search over the net and found nothing, yet.
I'm writing a bash script and I want to see what the script is really doing when being executed, like a log of all the commands, one by one, what are executed - this way, I'll be able to see why my script is falling.
Note : I've post similar question in the past, someone told me I run my bash script with sh -xe script.sh but it doesn't give me enough information to debug properly.
Please advise. Thanks!
Adding set -x at the beginning of the script displays, in the terminal, all the commands sent by the script as the terminal received it. It was exactly what I needed and it working perfectly.

why do we execute .sh scripts using the current directory notation ? [duplicate]

This question already has answers here:
Why do you need ./ (dot-slash) before executable or script name to run it in bash?
(9 answers)
Closed 4 years ago.
Our class on unix had an a question I did not know the answer to.
"why is it necessary to use ./XXX.sh to execute a bash executable?". I have not been able to find the answer on the web or in our textbook.
When you are invoking a command, the shell looks for the command in your $PATH variable, it will not look into your current directory. So you have to specifically tell the shell where your command is located. In this case, when the command or script is located in your current directory ., you need to invoke it using ./XXX.sh. In fact you can invoke scripts not inside your current directory using its full path e.g. /some/path/XXX.sh or relative to your current working directory e.g. ../../some/path/XXX.sh

How can I access a directory that is in $HOME using bash? [duplicate]

This question already has answers here:
"~/Desktop/test.txt: No such file or directory"
(2 answers)
Why isn't tilde (~) expanding inside double quotes? [duplicate]
(1 answer)
Closed 4 years ago.
I'm new to Linux as well as bash. I made a script that accesses a folder that is located in the home directory, but the script will not always be called from the home directory. The prompt I'm getting when calling it from any subdirectories specifies that it can not find the file.
Here's an example of what I'm trying to do:
for entry in "~/.directory"/*
do
echo "$entry"
done
If I place the script in a subdirectory of /home and try to call it, the script is unable to find the directory. I know it exists as if I run ls ~/.directory in the subdirectory it is able to find the files and print them with no problem. Is there a different way I should be trying to access the directory in the bash shell? Thanks!
Voted to close my question. It seems rather specific to me, and the general solution was something I found earlier and was also posted in the comments below. I'll figure it out eventually -
Only unquoted tildes are expanded.
for entry in ~/".directory"/*

why bash shell does not make any difference after executed? [duplicate]

This question already has answers here:
Global environment variables in a shell script
(7 answers)
Closed 5 years ago.
I'm working a small project which needs using OpenMPI to make "mpicc" work.
I made a file make_cmd:
#!/bin/bash
module load OpenMPI
However, after executing ./make_cmd, I was told:
mpicc: command not found
But if I just type on the command line: module load OpenMPI, then mpicc is working.
Why is that? Thanks!
See this answer on neighbouring site.
Because module is an alias/shell function and not a binary program, it's not necessarily available in the non-interactive sub-shell that is created when you run your script. You could probably run source make_cmd though, as that will just run the commands in your current interactive shell. You could ditch the #!/bin/bash line in that case.

Get path of where a script was called from [duplicate]

This question already has answers here:
How do I get the directory where a Bash script is located from within the script itself?
(74 answers)
Closed 5 years ago.
If I have a bash script in my home directory and call for it to execute from my Desktop, is it possible for the script to know the request to execute came from the Desktop? I've figured out how to get the path of where the script file is, but I want to know where the request came from.
Thanks!
Try this:
srcdir=$(pwd)
which gets you the current directory. That is, where you are calling your script from, which should be Desktop something in your example.
As duly discussed in the comments, this alternative is pretty good too:
srcdir=$PWD

Resources