Day 16: Run Commands in the Background

I was looking into configuration files for Vim today. I tried to read through them without using VS Code but it wasn’t easy.

TLDR;

Okay, so here are the highlights of what I did:

  • Looked into Vim config files and watched a few videos on dot files in general.
  • Learned how to run terminal commands in the background of the terminal. This was spurred by my desire to open a new terminal from within my current terminal.
  • Practiced using Vim alone. Honestly, it sucked. The biggest issue I had was the syntax highlighting. It was very particular which frustrated me.

Vim Progress

I tried to be brave today and only use Vim from the terminal without openeing VS Code. Let me tell you first hand that it sucked. I was editing my notes which I usually write in Markdown. The syntax highlighting was there but it was lacking for sure. Additionally, the custom code snippets I wrote for myself in VS Code were no longer available which made things that much slower. I am adjusting overall to using Vim but I am still far from comfortable using it as a standalone code editor. Maybe, after I get more comfortable with the terminal in general, I might have a chance.

How to Run a Terminal Command in the Background

Since I was trying to use Vim alone with VS Code, I figured I should have more than one terminal window open. However, here’s the catch… How do I open a new terminal inside of the existing terminal?

Well we can just execute the program again… Close but now the terminal that you used to run the command to start the next terminal is linked and logging STDOUT and STDERR.

So here is the trick. For myself I am on a Windows PC so I’m using Git Bash (don’t judge):

/git-bash.exe & > /dev/null/ 2>&1
# or
/git-bash.exe & &> /dev/null

The key parts are:

  • /git-bash.exe is the executable we need to run.
  • The first & after the command (git-bash.exe) tells the terminal to run the command in the background.
  • > , 2>&1&> are different ways to redirect the STDOUT and STDERR of a program. In our case we are throwing away the output since we don’t need it.

This actually helps me with a few other programs. I have had the issue that executing some programs from the terminal leaves the terminal useless while it outputs info from the program I started. I still lack a complete understanding but this was definitely a step in the right direction. Here is the table from my notes on Input and Output Redirection in the shell.

Shell operatorDescriptionOverwrite existing file?
command > output.txtSave terminal output (standard output) to a file named output.txtYes
command >> output.txtAppend terminal output (standard output) to a file named output.txtNo
commandTakes standard input from output.txt fileN/A
command 0Takes standard input from output.txt fileN/A
command 1> output.txtPuts standard output to output.txt fileYes
command 1>> output.txtAppends standard output to output.txtNo
command 2> output.txtPuts standard error to output.txtYes
command 2>> output.txtAppends standard error to output.txt fileNo
command &> output.txtPuts both standard error and output to output.txtYes
command > output.txt 2>&1{POSIX} Puts both standard error and output to file named output.txt. 2>&1 tells the error output ( 2> ) to go to the same place the standard output is going ( &1 ).Yes
command &>> output.txtAppends both standard error and output to file named output.txtNo
command >> output.txt 2>&1{POSIX} Appends both standard error and output to file called output.txtNo
command | tee output.txtPuts standard output to output.txt while displaying output on screenYes
command | tee -a output.txtAppends standard output to output.txt while displaying output on screenNo
command |& tee output.txtPuts both standard output and error to output.txt while displaying output on terminalYes
command 2>&1 | tee output.txt{POSIX} Puts both standard output and error to file named output.txt while displaying output on terminalYes
command |& tee -a output.txtAppend both standard output and error to file called output.txt while displaying output on terminalNo
command 2>&1 | tee -a output.txt{POSIX} Append both standard output and error to file named output.txt while displaying output on terminalNo
command &The & makes the command run in the background.No
command &&The && executes the first command and conditionally proceeds to the second if it exits with success.No

Conclusion

That’s all for today. If you are interested in the MIT course you can check out the video lecture I’m currently going through. The lecture is helpful but isn’t sufficient by itself. Anyways, until next time PEACE!