I am trying to understand the functioning of the Build Systems of Sublime Text 3. The commands going into the shell_cmd are the ones which will go directly into the terminal, but the terminal shows "No such file or directory exists". Here's the build system I made.

  "shell_cmd": "gnome-terminal -- bash -c '${file_path}/${file_base_name}; read -sn 1'",
  "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
  "working_dir": "${file_path}",
  "selector": "source.c, source.c++, source.cxx, source.cpp",
          "name": "Run",
          "shell": true,
          "shell_cmd": "gnome-terminal -- bash -c '${file_path}/${file_base_name}; read -sn 1'"

enter image description here

The actual file path is : '/home/dr_insult/COMPUTER PROGRAMS/DATA STRUCTURES & ALGORITHMS/Stacks & Queues/'. I know that it is caused because there is a space between words, but shouldn't that have been corrected by the '' quotes in the build system?

  • The quotes do not help here, because this is not interpreted by bash but by the json parser of sublime. You should escape the spaces and the &. – pLumo May 25 at 13:07

Your issue is that you need to encapsulate your path with quotes. It may look that you already are, but I guess you are getting confused by the number of "string in a string" you have.

Starting with your JSON, you are passing the value of the shell_cmd key to your program which is:

gnome-terminal -- bash -c '${file_path}/${file_base_name}; read -sn 1'

That command is starting the gnome-terminal application and asking it to run the following command on startup:

bash -c '${file_path}/${file_base_name}; read -sn 1'

That command, in turn, is executing bash and telling it to run the following command - Take note of the non-existing quotes:

${file_path}/${file_base_name}; read -sn 1

Before running, that command will replace the variable with their appropriate value before being executed, and according to the error message you showed, the command is becoming the following:

/home/dr_insult/COMPUTER PROGRAMS/DATA STRUCTURES & ALGORITHMS/Stacks & Queues/; read -sn 1

Finally, what is happening, in this case, is that bash is parsing that command by splitting it on the & character as it thinks it should run several commands in the backgrounds like so:

# This first command, for example, is attempting to run a script named
# /home/dr_insult/COMPUTER while passing two parameters
# "PROGRAMS/DATA" and "STRUCTURES" and attempting to put it in the background
read -sn 1

All of those paths don't exist in your system, which it raising those errors you see.

What you need to do is to quote your that command in order to tell bash to treat all that string as a single path.

So change the value of the shell_cmd key to the following (You will need to escape the double quotes so your JSON stays valid)

"gnome-terminal -- bash -c '\"${file_path}/${file_base_name}\"; read -sn 1'"
| improve this answer | |

Try to use script for testing:

file_path='/home/spacer/test/computer programs/data structures & algorithms/stacks & queues/'
echo $file_path$file_base_name
echo $file_path$file_base_name > /home/user/test/testpath.txt


Test it directly:

$ ./testpath.sh 
/home/user/test/computer programs/data structures & algorithms/stacks & queues/file.txt

Test it using a new instance of gnome-terminal:

$ gnome-terminal -e /home/user/test/testpath.sh
$ cat /home/user/test/testpath.txt
/home/user/test/computer programs/data structures & algorithms/stacks & queues/file.txt

In my case it works without issues.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.