Score:1

How I can run application from anywhere in the terminal without being in the full path

vu flag

I have an application that I can run while I am in the file location as the below:

-$ cd home/myApps/app
-$ ./run.sh

I want to call the run.sh from anywhere in my terminal without being in the full path, I tried to used alais and add the below code to bashrc file.

alias mylovleyapp=/home/myApps/app/run.sh

the problem is whenever I want to run the app using mylovleyapp, the program return error because the run.sh is depending on another files in myApps folder

How I can achieve that?

cocomac avatar
cn flag
The script probobly has a line that is `./something`. But that is relitive to _your_ current directory, not the current directory of the script. When you run it from where the script is, the two are the same, so it doesn't matter. But when you run it from somewhere else, it causes problems. Take a look at [this Stack Overflow question](https://stackoverflow.com/q/59895/16886597) for how to get the current directory of the script from inside the script. Update your script with that, and you should be good to go.
Will avatar
id flag
@cocomac maybe put this as an answer? I think it probably addresses the issue better than my answer.
cocomac avatar
cn flag
@Will I'll make it an answer in a few minutes
cocomac avatar
cn flag
@Will I made it a proper answer
Score:2
cn flag

Because the OP didn't provide a sample script, I'm going to make one. It will do one thing. Run ls in the directory of the script.

Yes, I know you can just do ls without the ./. But for this example, I'm pretending it is required, to demonstrate a script that uses the current directory of the script.

#!/usr/bin/env bash
ls ./

Save that script somewhere. I'll save it as /home/ubuntu/test/myscript. Make it executable with chmod +x /home/ubuntu/test/myscript. Now cd to the directory where the script is (e.g., cd /home/ubuntu/test), and run the script with ./myscript:

ubuntu@computer:~/test$ ./myscript
myscript someRandomFile

So far, so good. But now lets run it from a different directory. For this example, I'm going to run it from the root directory (/).

ubuntu@computer:~/test$ cd /
ubuntu@computer:/$ /home/ubuntu/test/myscript
bin  dev home lib   lib64  lost+found mnt proc run  snap sys usr
boot etc init lib32 libx32 media      opt root sbin srv  tmp var

Oops. We wanted it to print the files in the location of the script, not the location of the user. Before we figure out the solution, think about why it does this for a moment. The answer is simple. ./ is relative to the user's current directory, not the one of the script.

To get the directory of the script from inside of the script, we will look at this Stack Overflow question.

The gist of it is simple: script_dir="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" will get the current directory of the script, and store it in the variable script_dir.

Let's update the script accordingly. Our script is now this:

#!/usr/bin/env bash
script_dir="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
ls "$script_dir"

Let's run it, and see if it works:

ubuntu@computer:/$ /home/ubuntu/test/myscript
myscript someRandomFile

It works! It prints the files in the directory of the script, even when you are not in the same directory as the script, We're done. The overall idea is simple. In the script, ./ refers to the user's directory. "$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" refers to the directory the script is in. Update your script accordingly, and it should work.

Score:1
cn flag

You need to add your app path to PATH variable. To do that temporarily, go to the directory where you app is and then:

PATH=.:$PATH

You can now call your app from anywhere without having to type its full path. If you close the terminal this setting will be lost.

To set it permanently, you need to edit your .bashrc file and add :/home/myApps/app to the end of your PATH. Save the new .bashrc file and then:

source .bashrc
Score:1
cn flag

There are many ways to Rome.

Alias

If you prefer an alias, you can as follows:

alias mylovleyapp='(cd home/myApps/app && ./run.sh)'

The braces (...) cause the two commands to run in a subshell. Thus, your current directory will not have changed once the program has ended.

A wrapper script in your PATH

Instead, you can create a wrapper script and place that in a folder that is in your search PATH, e.g. ~/.local/bin or ~/bin if you are the only user needing access, or /usr/local/bin if all users need access. Then all you need to do is type the name of the script to launch the program.

The script can be just what you normally do to start the program:

#!/usr/bin/env bash
cd home/myApps/app
./run.sh

Save that script for example as ~/.local/bin/mylovleyapp. Make the script executable so it can be run:

chmod +x ~/.local/bin/mylovleyapp

If ~/.local/bin did not exist, and you had to create it, then log out and then back in to have it automatically included in your PATH. From now on, just typing mylovleyapp in the terminal will run your app.

MindC3ll avatar
vu flag
Thanks a lot vanadium for you answer! .. it worked as a charm .. I created A wrapper script to my path and that is just perfect.
Score:0
id flag

So you need to change your working directory to the one the target application is in, then execute the application so that it can find the dependent files.

I think you can do this by giving two separate commands in the alias: one to change directory and the other to execute the program, separated by a semi-colon.

So your .bashrc addition should read:

alias mylovleyapp='cd /home/myApps/app; ./run.sh'

That assumes the dependent files are in /home/myApps/app ... if not then you can change the working directory to the one with the dependent files, then run the app using an absolute path as the second component.

MindC3ll avatar
vu flag
aren't there any way to make the folder universal?
cocomac avatar
cn flag
@MindC3ll Yes there is. See [my comment](https://askubuntu.com/questions/1368492/how-i-can-run-application-from-anywhere-in-the-terminal-without-being-in-the-ful/1368496#comment2351406_1368492). It is more complex than Will's answer, but it will mean you can run the script without `cd`-ing to the directory
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.