Run program from anywhere without changing directory
Solution 1
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?
Solution 2
If you do not want the cd
to stick after the alias substitution, use a subshell with (
y )
:
alias my_x="(cd /home/path_to_x && ./x)&"
you can check it with
alias test_y="(cd /tmp && sleep 10 ) & "
Note that the solution
alias my_y="/home/path_to_x/x"
is not exactly equivalent. In fact, if called via my_x
, the x
program is run with a current directory /home/path_to_x/
, while if called by my_y
, x
is run with a current directory which is the one where the command my_y
was issued. This can be important or not depending on what x
is doing.
About the OP solution, it works in bash
:
romano@RRyS:~$ pwd
/home/romano
romano@RRyS:~$ alias x="cd /bin && ./echo A >/dev/null &"
romano@RRyS:~$ x
[1] 16611
romano@RRyS:~$ pwd
/home/romano
but not in zsh
:
[romano:~] % pwd
/home/romano
[romano:~] % alias x="cd /bin && ./echo A >/dev/null &"
[romano:~] % x
[1] 16744
[1] + 16744 done ./echo A > /dev/null
1& [romano:/bin] % pwd
/bin
[romano:/bin] %
It seems that bash and zsh execute lists in different ways ...so it's better to add the explicit parenthesis... thanks @EliahKagan for pointing it to me.
Solution 3
If the file which you want to run is already executeable, why don't you add it to you PATH variable?
If your executable file is /home/user/aplication/appX
just enter
PATH=$PATH:/home/user/application
to your ~/.bashrc
or ~/.profile
After restarting your console, you can run it simply with appX
I think this is the most clean solution.
Solution 4
Putting a &
at the end seems to do the trick:
alias x = "cd /home/path_to_x && ./x &"
Related videos on Youtube
TheFisherman
Updated on September 18, 2022Comments
-
TheFisherman over 1 year
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?
-
sourav c. over 9 yearsset an alias like
alias x='/home/path_to_x/x'
. Don't use.
before/home
..
(dot) refers to current directory. -
Adaephon over 9 yearsDoes
x
really need to be run while being in/home/path_to_x
? Or are you just unsure about how to run a program residing in a specific directory? -
c0rp over 9 yearspossible duplicate of Execute command with relative (upper) path. Using another working directory
-
c0rp over 9 yearsOne more similar question askubuntu.com/questions/427818/…
-
-
Rmano over 9 yearsAre you sure? After executing
x
you are in/home/path_to_x
. The&
at the end simply send the process in background. -
Eliah Kagan over 9 years@Rmano This actually works.
cd
s in background jobs don't affect the caller (which makes sense--it'd be really weird if they did). This is because asynchronous execution uses a subshell. Though this would be better if it explained why it works and what&
does--and how running in the background is sometimes undesirable--this is (already) a correct answer. (Explicitly making a subshell with( )
syntax, as you suggest, is probably a better solution most of the time though.) -
Rmano over 9 years@EliahKagan you are right. I checked the solution with
zsh
and effectively it works inbash
and not inzsh
... must be some difference in how the aliases are expanded. -
Rmano over 9 yearsAsked a question: unix.stackexchange.com/questions/162286/…
-
muru over 9 yearsYou misunderstood the effect of that Rmano's alias. The command line is not blocked. It's just that the output of the alias (
test
) pushed the cursor to the next line. You can still do whatever you want there (including pressing enter to open a fresh new prompt). -
muru over 9 years@Rmano did you enable the settings related to
pushd
inzsh
(I havesetopt autopushd pushdsilent pushdtohome
, and it remembers my cwd.) -
αғsнιη over 9 yearsyup, yes correct. @mure :|
-
Rmano over 9 years@muru
getopt autopushd pushdsilent pushdtohome
says-- pushdsilent pushdtohome
(I think it's the default) -
Eliah Kagan over 9 yearsGiving the trailing
&
higher precedence than&&
seems like it may be unique tozsh
. I triedsleep 5 && echo done &
inzsh
,bash
,ksh
,mksh
,dash
, and even one non-Bourne-style shell (tcsh
). Onlyzsh
evaluatedsleep 5
in the foreground; all the others returned immediately (then also printeddone
five seconds later, of course). On the other hand, even if this is entirely a peculiarity ofzsh
, I think adding explicit( )
(or{ ;}
) is reasonable, as it conveys the intent to other humans (or to oneself, when reading the script or history later). -
Dennis Williamson over 9 yearsThis is the correct answer for the question as posed.
-
Dennis Williamson over 9 yearsNote that since you use double quotes the variable will include the current directory at the time the alias is defined. Use single quotes to defer evaluation until the alias is expanded. However,
pushd
andpopd
do what your answer does.alias x='pushd /home/path_to_x && ./x && popd'
-
Kaz Wolfe over 9 yearsThis would only be incorrect if the program needed to be run in a specific directory that it gets from
pwd
-
Useless over 9 yearsAlthough you're right in general, for this specific question OP was visibly trying to run from the absolute path (just with a one-character mistake). However, I'm sure they can clarify if working directory is important in this case.