Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash build system for running currently open script in Sublime Text 2
{
"cmd" : ["$file"],
"selector" : "source.shell",
"shell" : "bash"
}
@pyrochlore

This comment has been minimized.

pyrochlore commented Feb 18, 2013

Thanks

@xavdid

This comment has been minimized.

xavdid commented Mar 26, 2013

I'm using this, but it's saying "permission denied". Do I need to change something in my bash profile or something else I'm not thinking of?

@shmup

This comment has been minimized.

shmup commented Apr 16, 2013

@Xavdidtheshadow chmod +x script.sh

@alex1704

This comment has been minimized.

alex1704 commented Apr 28, 2013

cool)

@andrewheiss

This comment has been minimized.

andrewheiss commented Aug 8, 2013

This doesn't work if the script is in a folder with spaces in its name. Building ~/Desktop/Test Folder/test.sh will fail:

/bin/sh: ~/Desktop/Test: No such file or directory
[Finished in 0.0s with exit code 127]
@andrewheiss

This comment has been minimized.

andrewheiss commented Aug 8, 2013

However, it seems like you can fix that by modifying the build system slightly. Instead of specifying "shell": "bash", you can manually pipe it through bash, like so:

{
    "cmd"       : ["bash", "$file"],
    "selector"  : "source.shell"
}
@gaiatronik

This comment has been minimized.

gaiatronik commented Aug 16, 2013

Thank you Andrew, that worked perfectly.

@raisen

This comment has been minimized.

raisen commented Sep 24, 2013

+1

@mehikmat

This comment has been minimized.

mehikmat commented Feb 28, 2014

I run a sh file using above build script and run smoothly but waits at some point and doesn't take input if I type.

rm: remove write-protected regular file `/etc/udev/rules.d/70-persistent-net.rules'?

I typed y but didn't take any action.

Any Idea?

@farinspace

This comment has been minimized.

farinspace commented Jun 30, 2014

In sublime text 3, I am using the following:

{
    "cmd"       : ["$file"],
    "selector"  : "source.shell",
    "path"      : "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"
}

Note: I had to add "path" above, as the terminal in Sublime was using a different PATH, my other attempts at adjusting my PATH were unsuccessful. Also see the build system reference for more details.

@MadSc1

This comment has been minimized.

MadSc1 commented Jan 12, 2015

I also had to add the line as suggested above,
"path" : "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"
Even though I already set up my symbolic links in terminal properly = Yes, I can run "python3" from any location...
To hammer it in farther... When I type
"echo $PATH"
it returns
"/Library/Frameworks/Python.framework/Versions/3.4/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/Hendrix/bin"

Yes, I am new at development. However, from logical deduction I ponder if sublime text 3 is not using ~/.bash_profile, or the system paths(??did I say that right?) as we might be drawn to assume... Then again... I am still at noob level, ,, but ya, adding the "path" line as suggested by 'farinspace' did the trick for me - OS X 10.9.5

@muktyas

This comment has been minimized.

muktyas commented Aug 6, 2015

Hi All, I new on sublime. How to add that code into sublime?
Edit:
Okay, tools, build system, new build system.. and replace all content to andrewheiss's code. Thanks.

@hanie2014

This comment has been minimized.

hanie2014 commented Dec 9, 2015

thanks

@pudovmaxim

This comment has been minimized.

pudovmaxim commented Oct 5, 2016

For some language (for example russian) whitespace symbols can be found in path:
/home/username/Рабочий стол/test.sh
and "cmd" : ["$file"], not works correctly. I recommend add quotes and chmod for set permissions
"cmd" : ["chmod +x \"$file\" && \"$file\"" ],

@JosjaVanBever

This comment has been minimized.

JosjaVanBever commented Dec 1, 2018

If one wants to also have a "debug mode", a simple variant is

{
    "cmd": ["bash", "$file"],
    "selector": "source.shell",

    "variants":
    [
        {
            "name": "Debug",
            "shell_cmd": "/bin/bash -xv \"${file}\""
        }
    ]
}

note the escaped quotation marks to avoid issues with spaces etc. in the file name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment