Writing Scripts in VS Code

VS Code is a free editor for Windows, Mac and Linux; code . opens the folder you're in, the free ShellCheck extension underlines mistakes in a script as you type, and its built-in terminal runs the script without leaving the editor, though nano still works.

Apprentice · Unix · Linux · Updated

After this lesson you can

  • Open a folder of scripts in VS Code with code . and run a script in its built-in terminal
  • Read a ShellCheck warning in VS Code, such as SC2086, and fix the line it underlines

Key takeaways

  • VS Code is a free code editor for Windows, Macs and Linux. It's optional; nano still does the job.
  • code . opens the folder you're in, with its files listed on the left. In WSL, it opens VS Code on Windows, connected to Ubuntu.
  • The ShellCheck extension, also free, underlines mistakes in a script as you type. Point at the underline to read what's wrong and its code, such as SC2086.
  • Terminal, then New Terminal, opens a terminal inside the editor, in the same folder. Save with Ctrl+S before you run the script.

nano is fine for a dozen lines. But the scripts in this module grow: a backup that checks before it copies, a loop over thirty files, a menu. At that size you want what a word processor gives you for an essay: colors that set the parts apart, line numbers, the mouse, and a spell checker that underlines a mistake before you hand it in. For scripts, that's VS Code with the ShellCheck extension. Both are free, and both are optional: if nano suits you, keep using it. Every lesson shows a script's contents, not the editor it was typed in.

VS Code: one free editor for every computer

VS Code (Visual Studio Code) is a free code editor from Microsoft, and one of the most widely used. Download it from its own website, code.visualstudio.com, for your computer:

  • Windows, with Ubuntu in WSL: install the Windows version. Don't install a second copy inside Ubuntu: the Windows one works with Ubuntu's files.
  • A Mac: drag it into Applications, open it, press Cmd+Shift+P, type shell command, and choose Install 'code' command in PATH. That gives the terminal a code command.
  • Ubuntu's desktop: the .deb file from the website installs it, code command included.

code . opens the folder you're in

In the terminal, go to your scripts and open the whole folder, . being "this folder":

cd ~/terminal-practice/morning
code .

VS Code opens with the folder's files listed on the left: morning.sh and todo.txt. Click morning.sh to open it. Comments are one color, commands another, text in quotes a third, and every line has a number, so an error that says "line 10" takes a second to find.

In WSL, code . opens VS Code on Windows, connected to Ubuntu: the first time, it sets up a small helper inside Ubuntu, which takes a minute. The bottom left corner of the window then says WSL: Ubuntu.

ShellCheck: a spell checker for scripts

ShellCheck is a linter: a program that reads your code without running it and points out lines that are likely mistakes. Its VS Code extension, also free, brings ShellCheck with it, so there's nothing else to install. Click the Extensions icon on the left (or press Ctrl+Shift+X, Cmd+Shift+X on a Mac), search for ShellCheck, and click Install. In WSL, the button says Install in WSL: Ubuntu.

Now try a change that looks harmless. Keep the to-do file's path in a variable at the top of morning.sh (Variables in the Shell), and use it on the cat line:

todo=~/terminal-practice/morning/todo.txt
cat $todo

ShellCheck underlines $todo with a wavy line as you type. Point at it with the mouse, and VS Code shows ShellCheck's message, Double quote to prevent globbing and word splitting, and its code, SC2086.

VS Code with morning.sh open: the line cat $todo has a wavy underline under $todo, and a box above it shows ShellCheck's message "Double quote to prevent globbing and word splitting" with the code SC2086

The script still works today. The warning is about the day your file is called to do.txt, with a space. At the prompt, you can see what bash would do:

todo="to do.txt"
cat $todo
cat "$todo"
cat: to: No such file or directory
cat: do.txt: No such file or directory
pay the phone bill
finish the history essay
buy coffee beans

Without quotes, bash split the name at the space and cat looked for two files. With quotes, "$todo" stays one name. So the fix is the one ShellCheck asks for: cat "$todo". Quotes: Double, Single and None, in the next chapter, explains why. Often a light bulb appears beside the warning, offering to make the fix for you. Each code, such as SC2086, also has its own page on ShellCheck's website, with an example and the fix: search for the code.

A terminal inside the editor

You don't need to switch windows to run what you wrote. Choose Terminal, then New Terminal, or press Ctrl and the backquote key, `, above Tab. A terminal opens under the script, already in the folder you opened, and in WSL it's Ubuntu's bash. Type ./morning.sh there, and its output appears right under your code.

One habit matters: save first, with Ctrl+S (Cmd+S on a Mac). The terminal runs the file as it is on disk, and a file with unsaved changes shows a dot on its tab instead of an X.

Check yourself

2 questions to try on the lesson page.

So what

VS Code shows your script in color, ShellCheck catches a mistake before it bites, and the terminal sits right under the code. It's a more comfortable desk, not a different job: everything you write runs the same as it did from nano.

Key terms

Cheat sheets

Related