3

There is a .desktop compilation script, that needs to remain open after terminating, in case there are errors I need to correct. In Windows, I would run cmd /k script.bat, and the /k would keep a cmd window open after terminating.

In Linux, my solution is to create a bash script, that consists of the "compile" command, and a "bash" command at the end. The .desktop file invokes the script, which in turn stays open after finishing, due to the bash command.

Is there a way to do this with a single file? If so, I need to do either one of the following with a single command, e.g. by a command-line argument to bash:

  • Make bash run a command before starting (equivalent of cmd /k command in Windows)

  • Make bash start in a certain directory (I can configure the compilation to trigger when its directory is opened)

  • Make the .desktop file stay open after executing its command (this is the least preferable, since I would need a second window open to correct mistakes, if any)

Please let me know if one of these is possible.

3

I found a way. I got it from some stackexchange site but can't find it back.

Anyway: You can abuse bash's --init-file option. It is intended to override using some other startup script instead of .bashrc, but you can put anything there. Combined with anonymous pipes, you can do something like this:

bash --init-file <(echo "your --command --here")

After the command ends, you'll be dropped back into bash - not a new instance of bash like with some other solutions, but the same instance.

Now, this'll skip .bashrc of course because that's what --init-file is for. To fix that, you get the tremendously pretty

bash --init-file <(echo ". \"$HOME/.bashrc\"; your --command --here")

Ugly, but it works :-)

On the Windows Subsystem for Linux

Finally, I tried doing this from Windows, but got into trouble because when you start bash to invoke the Windows Subsystem for Linux, you're still in Windows land so anonymous pipes won't work, $HOME is all wrong, and so on. My workaround for "cmd /k, but for starting the windows subsystem for linux" has become:

bash -c 'exec bash --init-file ^<(echo ". \"$HOME/.bashrc\"; your --command --here")'

Note the ^ to escape the < character. This will start bash (bash -c), immediately replace the current bash process by a new one (exec), which is also a bash process, now initialized as discussed before, from within Linux land.

I made a batch file called bash-k.cmd in my PATH so i can invoke it easily, with this inside it:

@bash -c 'exec bash --init-file ^<(echo ". \"$HOME/.bashrc\"; %*")'

It allows me to do bash-k cowsay hello and things will work

0

Thank you @skrebbel that is a very helpful command, here is are .reg file codes to add a Bash Console Here right-click context menu if you have Windows Subsystem for Linux

AddContext.reg:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\BashConsoleHere]
@="Bash Console Here"
"Icon"=hex(2):25,00,4c,00,4f,00,43,00,41,00,4c,00,41,00,50,00,50,00,44,00,41,\
  00,54,00,41,00,25,00,5c,00,6c,00,78,00,73,00,73,00,5c,00,62,00,61,00,73,00,\
  68,00,2e,00,69,00,63,00,6f,00,00,00

[HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\BashConsoleHere\command]
@="bash -c 'exec bash --init-file <(echo \". \"$HOME/.bashrc\"\")'"

[HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\BashConsoleHere]
@="Bash Console Here"
"Icon"=hex(2):25,00,4c,00,4f,00,43,00,41,00,4c,00,41,00,50,00,50,00,44,00,41,\
  00,54,00,41,00,25,00,5c,00,6c,00,78,00,73,00,73,00,5c,00,62,00,61,00,73,00,\
  68,00,2e,00,69,00,63,00,6f,00,00,00

[HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\BashConsoleHere\command]
@="bash -c 'exec bash --init-file <(echo \". \"$HOME/.bashrc\"\")'"

[HKEY_CLASSES_ROOT\Drive\shell\BashConsoleHere]
@="Bash Console Here"
"Extended"=""
"NoWorkingDirectory"=""

[HKEY_CLASSES_ROOT\Drive\shell\BashConsoleHere\command]
@="bash -c 'exec bash --init-file <(echo \". \"$HOME/.bashrc\"\")'"

Undo.reg:

Windows Registry Editor Version 5.00

[-HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\BashConsoleHere]
@="Bash Console Here"
"Icon"=hex(2):25,00,4c,00,4f,00,43,00,41,00,4c,00,41,00,50,00,50,00,44,00,41,\
  00,54,00,41,00,25,00,5c,00,6c,00,78,00,73,00,73,00,5c,00,62,00,61,00,73,00,\
  68,00,2e,00,69,00,63,00,6f,00,00,00

[-HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\BashConsoleHere\command]
@="nircmd elevate cmd /c cd \"%V\" && bash.exe"

[-HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\BashConsoleHere]
@="Bash Console Here"
"Icon"=hex(2):25,00,4c,00,4f,00,43,00,41,00,4c,00,41,00,50,00,50,00,44,00,41,\
  00,54,00,41,00,25,00,5c,00,6c,00,78,00,73,00,73,00,5c,00,62,00,61,00,73,00,\
  68,00,2e,00,69,00,63,00,6f,00,00,00

[-HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\BashConsoleHere\command]
@="nircmd elevate cmd /c cd \"%1\" && bash.exe"

[-HKEY_CLASSES_ROOT\Drive\shell\BashConsoleHere]
@="Bash Console Here"
"Extended"=""
"NoWorkingDirectory"=""

[-HKEY_CLASSES_ROOT\Drive\shell\BashConsoleHere\command]
@="nircmd elevate cmd /c cd \"%1\" && bash.exe"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.