I have this simple bash script
#!/bin/bash
echo "Hello, World!"
I love how we can can right click on python code, and run it in a Terminal.
But I can’t find way to do the same for bash scripts.
Can someone help?
I open terminal tab, and copy file path, and type ./filePath.sh
.
But my goal is to find a more efficient way.
2
Answers
Terminal: Run Active File In Active Terminal
There’s the
Terminal: Run Active File In Active Terminal
command, which you can run from that command palette. That’s either three or one user actions: It’s three if you open the command palette via keyboard shortcut, type out part of the command name, and then press enter, but it’s one if you bind the command to a keyboard shortcut (the command ID isworkbench.action.terminal.runActiveFile
). This will work for your case because you have a shebang in your script file. Just make sure you have execute permissions for the script (you can add such a permission withchmod +x file.sh
).Copy relative path and paste into terminal
You could just open up an integrated terminal in VS Code with a Bash shell, (which VS Code will open and navigate the current working directory to the workspace folder’s path), and then just do
./relative/path/to/script.sh
. You can use command palette commands or keyboard shortcuts to open up the terminal too (View: Toggle Terminal
, bound to ctrl+` by default on Windows and Linxu), and also to copy the relative path to the file (relative to the workspace folder) (File: Copy Relative Path of Active File
bound to ctrl+shift+alt+c by default on Windows and Linux). So this is approximately four actions: copy path, open terminal, paste, hit enter.Task
You can write a task for it:
You can run tasks with the
Tasks: Run Task
command in the command palette, or associate a keyboard shortcut to it (workbench.action.tasks.runTask
). If you bind a keyboard shortcut, this’ll be approximately two to three actions: open the tasks selection menu, select the task (no need if it was the last task you used- that’ll be selected by default), hit enter.Extensions
You can also take a look at installing extensions related to Bash. One extension I’ve heard people use for running things (but don’t use myself) is
formulahendry.code-runner
. Just searching "Bash" in the Extension View, I also sawdominic-valerio.bash-runner
.Hit ctrl+shift+p, and begin typing in
Terminal: Run Active File In Active Terminal
.If It says
permission denied
or similar, you may have to make it executable:Note that this only has to be done once, if at all.