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.
alias x='/home/path_to_x/x'
. Don't use.
before/home
..
(dot) refers to current directory.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?