Thursday, April 25, 2024
14
rated 0 times [  14] [ 0]  / answers: 1 / hits: 22528  / 3 Years ago, tue, may 18, 2021, 4:34:50

I wish to create an alias, which runs program x, without changing the current directory I am in. What I have now is:



alias x = "cd /home/path_to_x && ./x"


This works, but changes my directory. What I wish to do is something like:



alias x="./home/path_to_x/x


But then I get no such file or directory. Anyone know a workaround for this?


More From » command-line

 Answers
4

DON'T CD, just run it using its absolute path



This version:



cd /home/path_to_x && ./x


changes directory to an absolute path (you see how /home/... starts at the root directory) and then runs the executable at the relative path ./x (that is, relative to the new working directory).



This version:



./home/path_to_x/x


tries to run the executable at the relative path ./home/path_to_x/x, which means relative to whatever your current working directory is now. That explains why you get the error - this relative path really doesn't exist.



The command you want would be:



/home/path_to_x/x


using the absolute path (starting at the root directory /) again.



Oh, and you can also just add /home/path_to_x to your PATH instead of creating the alias. See: How to run scripts without typing the full path?


[#22823] Wednesday, May 19, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
whoppinolo

Total Points: 93
Total Questions: 113
Total Answers: 107

Location: Cyprus
Member since Mon, Oct 24, 2022
2 Years ago
;