keepcalculator

06 Numbers and Powers

How to Choose a Shell and Run Commands in VS Code

Card No. 06·33090 · Updated · Standard formula, shown below

This guide shows you how to select which shell (like PowerShell, Command Prompt, or Bash) VS Code uses for its integrated terminal, and how to run commands inside it. You’ll learn how to switch shells per workspace, change the default, and execute commands efficiently. No prior terminal experience is needed, but basic familiarity with VS Code’s interface helps.

What You’ll Need

  • Visual Studio Code installed (any recent version).
  • At least one shell available on your system: Windows has PowerShell and Command Prompt; macOS and Linux come with Bash (and often Zsh).
  • Optional: Git Bash, WSL, or other shells you may have installed.

Step-by-Step

  1. Open the Integrated Terminal: In VS Code, press Ctrl+` (backtick) on Windows/Linux or Cmd+` on macOS. Alternatively, go to the menu View → Terminal. This opens a terminal panel at the bottom of the window.
  2. See the Current Shell: The terminal opens with your default shell. Look at the right side of the terminal panel’s title bar—you’ll see the shell name (e.g., “powershell”, “bash”, “cmd”).
  3. Choose a Different Shell for This Session: Click the dropdown arrow next to the + icon in the terminal panel’s top-right corner. A list of available shells appears. Click any shell to open a new terminal tab with that shell. This does not change your default—it’s just for this session.
  4. Change the Default Shell (Windows): Open the Command Palette with Ctrl+Shift+P (or Cmd+Shift+P on macOS). Type Terminal: Select Default Profile and press Enter. A list of detected shells appears. Select the one you want as the default. The next time you open a terminal, it will use this shell.
  5. Change the Default Shell (macOS/Linux): The same steps work: Command Palette → Terminal: Select Default Profile. If your shell isn’t listed, you may need to add it manually (see Tips).
  6. Run a Command: Type a command in the terminal and press Enter. For example, type echo hello and press Enter—you’ll see “hello” printed. Commands are executed in the current directory (shown at the prompt).
  7. Run a Command in a Specific Shell: If you want to run a command in a different shell without opening a new tab, you can use the shell’s syntax directly. For instance, in PowerShell, you can run bash -c "ls" to execute a Bash command. But it’s usually cleaner to open a new tab with the desired shell.
  8. Kill or Restart the Terminal: To stop a running command, press Ctrl+C (or Cmd+C on macOS). To restart the terminal (e.g., after changing settings), click the trash can icon in the terminal panel’s top-right corner, then open a new terminal.

Common Mistakes

  • Using the wrong command syntax: Each shell has its own syntax. For example, ls works in Bash and PowerShell (as an alias), but dir is for Command Prompt. If a command fails, check which shell you’re in.
  • Forgetting to save settings: When you change the default profile, the change takes effect immediately for new terminals, but if you have an existing terminal open, it won’t switch. Close and reopen the terminal to see the change.
  • Selecting a shell that isn’t installed: The list of profiles only shows shells VS Code detects. If you pick one that’s broken, the terminal may fail to open. Always test a shell before making it default.
  • Running GUI commands in the terminal: Some commands open graphical applications (like code . opens VS Code). They may block the terminal until closed. Use & (in Bash) or Start-Process (in PowerShell) to run them in the background.

Tips

  • Add a custom shell: If your shell isn’t listed, you can add it manually. Open File → Preferences → Settings (or Ctrl+,), search for terminal.integrated.profiles.windows (or .linux/.osx), and edit the JSON to include the path to your shell executable.
  • Use keyboard shortcuts: Ctrl+Shift+` opens a new terminal with the default profile. Ctrl+Shift+5 splits the terminal. Ctrl+PageUp/PageDown switches between terminal tabs.
  • Set a default for a workspace: You can create a .vscode/settings.json file in your project folder and add "terminal.integrated.defaultProfile.windows": "bash" (adjust for your OS) to force a specific shell for that project.
  • Check the shell’s environment: When you switch shells, the environment variables (like PATH) may differ. If a command isn’t found, it might be because the shell doesn’t have the right PATH. Use echo $PATH (Bash) or $env:PATH (PowerShell) to inspect.
  • Use the terminal’s dropdown to see all open shells: The dropdown next to the + icon also shows all active terminal tabs; you can jump between them quickly.

Troubleshooting

  • Terminal doesn’t open or shows an error: This often means the selected shell isn’t found. Go to Terminal: Select Default Profile and choose a known-good shell like PowerShell or Bash. If that fails, check your VS Code installation—you may need to reinstall or update.
  • Command not recognized: The command may be a shell built-in or in a different PATH. Try using the full path (e.g., /usr/bin/python). Also, ensure you’re in the correct shell—dir won’t work in Bash.
  • PowerShell execution policy blocks scripts: On Windows, you might see an error about running scripts. You can change the policy with Set-ExecutionPolicy RemoteSigned -Scope CurrentUser in an administrator PowerShell, but be cautious—this affects security.
  • Shell opens but is slow or unresponsive: This can be due to a slow startup script (like a large .bashrc). Try opening a different shell to see if the issue persists. You can also disable shell integration in VS Code settings: terminal.integrated.shellIntegration.enabled to false.
  • Changes to default profile don’t stick: Ensure you’re selecting the profile from the Command Palette, not just opening a new terminal. Also, check if your workspace has a settings.json that overrides the default.

Frequently Asked Questions

How do I change the default shell in VS Code?

Open the Command Palette (Ctrl+Shift+P), type Terminal: Select Default Profile, and choose your preferred shell. New terminals will use it.

Can I use a different shell for each project?

Yes. Create a .vscode/settings.json in your project folder and add a line like "terminal.integrated.defaultProfile.windows": "bash" (adjust for your OS). This overrides the global default for that workspace.

Why does my terminal show a different shell than the one I selected?

If you have multiple terminal tabs open, each may have a different shell. Check the tab title or the dropdown. Also, if you changed the default, existing terminals won’t change—open a new one.

How do I run a command as administrator in VS Code terminal?

In Windows, you can run Start-Process powershell -Verb RunAs to open an elevated PowerShell window. In macOS/Linux, use sudo before the command, but be careful—it requires your password and can be risky.

Can I set environment variables for the terminal?

Yes, you can set them in your shell’s startup file (like .bashrc or .zshrc) or use the terminal.integrated.env.windows setting in VS Code to add variables for all terminals.

Why is the terminal not showing colors or formatting?

This may be due to the shell’s color scheme or VS Code’s theme. Try setting terminal.integrated.allowChords to false if keyboard shortcuts interfere. Also, ensure your shell supports ANSI colors (most do).

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →