Ok, I think I have a clue what is going on.
The behaviour you are observing in zsh and bash when executing echo $(mc)
is caused by mc
.
When you run mc
normally, it doesn't react when Ctrl+c is pressed, since it ignores SIGINT
. The way to end mc
is by pressing F10 and Enter.
When you run echo $(mc)
the input goes to the mc
process, so it is no wonder that when you press Ctrl+c nothing will happen, because mc
ignores SIGINT
.
But when you run echo $(mc)
and press F10 and Enter it will react.
(When I then press F10 and Enter again it actually quits; it should quit at the first try but I have no idea why it does not.)
From this fact I deduce that Midnight Commander is running normally, but the output from it is put into the shell buffer, so it can later be used by echo
.
Something we should consider normal.
Also you think that bash and zsh are hanging, but I would say that they are not hanging but waiting for the echo
command to return.
But echo
can only return when mc
returns, which it only does when F10 and Enter are pressed.
With this we can also explain what happens when you press Ctrl+z.
By pressing the combination mc
goes to sleep like it would if you had run mc
normally and echo
is still waiting for the sleeping mc
.
So everything is normal behavior.
Except for what fish does.
fish seems to start commands run with echo ($command)
in the background.
It kind of makes sense, since such a command normally does not need any input.
For the why and how I have no answer.
But you can see that it runs in the background when you enter
echo (mc)
jobs
If your intent is to get the working directory of a process, this is one way:
~ » jobs -l
[1] + 14308 running sleep 1h
~ » readlink /proc/14308/cwd
/home/matti-nillu
The following function inside .bashrc
, will do exactly what you want
cdjob ()
{
pid=$(jobs -p $1);
d=$(readlink /proc/$pid/cwd);
cd "$d"
}
Example:
~$ sleep 1h &
[1] 15102
~$ jobs
[1]+ Running sleep 1h &
~$ cd /
/$ cdjob %1
~$
Best Answer
Convert bash aliases to bash scripts
I decided to do this instead of approach below, and putting the scripts into
~/bin/
, which is in myPATH
. This makes it possible to use them from any shell, and it prevents potential problems with quoting. It skips recursive aliases likealias ls='ls -la'
, because we would get infinite recursion when using those as a script! As a workaround, use the absolute path in your alias, likealias ls='/bin/ls -la'
.Usage
Script
Deprecated: Creating fish wrappers that execute bash code
Below is a script that creates fish script wrappers for the local bash aliases: For each bash alias, it takes the contents, and creates a fish alias/script that executes the code in bash sub-shell. It is not optimal, but is sufficient for most of my aliases.
WARNING It might happen that the imported function acts differently then in bash. You may loose data or accidentally DDOS your coworkers when using them.
Usage (Deprecated)
Script (Deprecated)
save this in
~/.config/fish/functions/import_bash_aliases.fish
: