Sublime Forum

Is that possible to append date or other bash cmd in .sublime-build?

#1

“cmd”: [
“/usr/local/bin/sass”,
“–style=compressed”,
“$file”,
“$file_path/$file_base_name.$(date ‘%y%m%d’).css”
]

I tried to add bash date function but no luck.

0 Likes

#2

When you use cmd, you’re telling Sublime “directly execute the program in the first argument”, which in your case is /usr/bin/sass. All of the other items in the cmd list are passed to the executed program as arguments, so unless sass (in your case) knows to do something special with $(date), this won’t work for you (also it won’t work in general because of the reasons outlined below).

If you want to do something bash related (or shell related in general), then you want to use shell_cmd instead; that takes as an argument a string that represents what you’d type directly into a shell, where something like $(date) will work.

There are a couple of other issues with what you have here. The first is that $ is special in build systems because it’s used to specify variables to expand, like the $file that you’re using. Trying to use it like you are here is thus an issue because Sublime doesn’t know that it should leave the $ in $(date) alone.

The other issue is that the call to date is not correct; in order to get it to output the current date using your format, you need to prefix the format with a + character; otherwise date will generate an error because it thinks you’re trying to set the date instead:

tmartin:dart:~> date %y%m%d                                                                                                                                                                                                                               
date: invalid date ‘%y%m%d’
tmartin:dart:~> date +%y%m%d                                                                                                                                                                                                                              
191017

Taken all together, to do what you want you want to replace the cmd line you’re using with something like the following:

"shell_cmd": "/usr/local/bin/sass --style=compressed \"$file\" \"$file_path/$file_base_name.\\$(date +'%y%m%d').css\""

This passes the string here directly to bash to execute. All of the filenames are wrapped in \" so that if you have a file with a space in it, the command will still work. We also specify \\$ in front of (date) to tell Sublime to leave the $ character alone so that bash can see it, and the format string passed to date has the required + prefix.

1 Like